Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notify once the audio is finished playing

Well i am trying to implement basic functionality of voice recording , like

Record , play/pause , stop

i am able to do all of them but the only issue is how can i get notified once the audio gets finished playing. I mean if i play an audio file then once it gets finished playing i want a notification that it is stopped now.

so far i have used

   mPlayer.start() // to start the audio

   mPlayer.stop(); // to stop the audio

   mPlayer.pause(); //to pause the audio

i am just trying to find out how would i know once the audio file automatically finished playing

like image 261
Hunt Avatar asked May 10 '12 07:05

Hunt


3 Answers

You can use the Completion Listener of Media Player class to do this.

mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {

                Log.i("Completion Listener","Song Complete");
                Toast.makeText(context, "Media Completed", Toast.LENGTH_SHORT).show();
            }
        });
like image 190
Andro Selva Avatar answered Oct 19 '22 10:10

Andro Selva


Try to use this code

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >
    <Button id="@+id/cmd_play"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Play the music !!!"
       />
    </LinearLayout>

MusicPlayer Activity code

    public class MusicPlayer extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);

            // Find the Button from the xml-file.
            Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
            cmd_play.setOnClickListener(new OnClickListener(){

                            @Override
                            public void onClick(View arg0) {
                                    MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
                                                    R.raw.everlast);
                                    mp.prepare();
                                    mp.start();
                                    // i.e. react on the end of the music-file:
                                    mp.setOnCompletionListener(new OnCompletionListener(){

                                            // @Override
                                            public void onCompletion(MediaPlayer arg0) {
                                                    // File has ended !!! 

Toast.makeText(context, "Media Completed with Success", Toast.LENGTH_SHORT).show();
                                            }
                                    });
                            }
            });
        }
    }

put a file sound at assets folder

like image 28
K_Anas Avatar answered Oct 19 '22 11:10

K_Anas


use setOnCompletionListener method of mediaplayer class..See how to use that method.. here

like image 45
5hssba Avatar answered Oct 19 '22 10:10

5hssba