notify once the audio is finished playing

有些话、适合烂在心里 提交于 2021-02-18 10:56:25

问题


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


回答1:


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();
            }
        });



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/10529226/notify-once-the-audio-is-finished-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!