Notify after sound is finished

Well, I'm trying to implement basic voice recording functions, for example

Record, play / pause, stop

I can do everything, but the only problem is how can I get a notification after completing the audio play. I mean, if I play an audio file, and then when it finishes the game, I want the notification to be paused.

So far i have used

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

I'm just trying to figure out how I know how the audio file automatically ends

+7
source share
3 answers

You can use the Listening Listener of the 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(); } }); 
+24
source

Try using 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(); // ie 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(); } }); } }); } } 

enter the sound of the file into the folder folder

+5
source

use the setOnCompletionListener method of the mediaplayer class .. See how to use this method .. here

+2
source

All Articles