【问题标题】:notify once the audio is finished playing音频播放完毕后通知
【发布时间】:2012-05-10 07:23:29
【问题描述】:

我正在尝试实现语音录制的基本功能,例如

录音、播放/暂停、停止

我能够做到所有这些,但唯一的问题是我如何在音频完成后收到通知 玩。我的意思是,如果我播放一个音频文件,那么一旦它完成播放,我想要通知它现在已停止。

到目前为止我用过

   mPlayer.start() // to start the audio

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

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

我只是想知道一旦音频文件自动播放完毕,我怎么知道

【问题讨论】:

    标签: android android-mediaplayer


    【解决方案1】:

    您可以使用 Media Player 类的 Completion Listener 来执行此操作。

    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】:

    尝试使用此代码

        <?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 活动代码

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

    在资产文件夹中放一个文件声音

    【讨论】:

    • 这很有趣,我实际上使用了我找到的代码,它就像一个魅力。谢谢!
    【解决方案3】:

    使用媒体播放器类的setOnCompletionListener方法..See how to use that method.. here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多