Tuesday, August 1, 2017

android - Media Player on completion listener example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/btn_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private LinearLayout mRootLayout;
    private Button mButtonPlay;

    private MediaPlayer mPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mRootLayout = findViewById(R.id.root_layout);
        mButtonPlay = findViewById(R.id.btn_play);

        // set layout background color
        mRootLayout.setBackgroundColor(Color.GREEN);

        // Click listener for playing button
        mButtonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // If media player another instance already running then stop it first
                stopPlaying();

                // Initialize media player
                mPlayer = MediaPlayer.create(mContext,R.raw.snow_deep_movement);

                /*
                    void setOnCompletionListener (MediaPlayer.OnCompletionListener listener)
                        Register a callback to be invoked when the end of a media
                        source has been reached during playback.

                    Parameters
                        listener : MediaPlayer.OnCompletionListener: the callback that will be run
                */
                mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        // Do something when media player end playing
                        Toast.makeText(mContext,"Media Player End.",Toast.LENGTH_SHORT).show();

                        // Change the layout background color
                        mRootLayout.setBackgroundColor(Color.RED);

                        // Enable play button
                        mButtonPlay.setEnabled(true);
                    }
                });

                // Start the media player
                mPlayer.start();
                Toast.makeText(mContext,"Media Player is playing.",Toast.LENGTH_SHORT).show();

                // Change layout background color
                mRootLayout.setBackgroundColor(Color.GREEN);
                
                // Disable play button
                mButtonPlay.setEnabled(false);
            }
        });
    }

    // Custom method to stop media player
    protected void stopPlaying(){
        // If media player is not null then try to stop it
        if(mPlayer!=null){
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
            Toast.makeText(mContext,"Stop playing.",Toast.LENGTH_SHORT).show();
        }
    }
}