onCompleteListener之后无法播放下一首歌曲

时间:2019-05-14 03:24:03

标签: java android path android-mediaplayer

关于此问题,我已经阅读了许多不同的答复,但我只是想澄清一下MediaPlayer的onCompletionListener方法。我的配置当前正在运行,并且在完成第一首歌曲后会播放一​​首歌曲,但是第二首结束后,它将不再播放阵列中的另一首歌曲。也不会显示新曲目的元数据。我的猜测是onCompletionListener仅运行一次,并且不会传输到新创建的MediaPlayer实例。更具体地说,我正在寻找的答案是onCompleteListener可以运行多次吗?还是非循环功能?

展示问题的视频:https://youtu.be/WXDtCktTh3M

这是我的侦听器模块的内容:

{
  "results": [
    {
      "docs": [
        {
          "id": "apple1",
          "source": "dd"
        },
        {
          "id": "apple2",
          "source": "aa"
        },
        {
          "id": "apple3",
          "source": "dd"
        }
      ],
      "group": 99806
    }
  ]
}

更新

之所以不起作用,是因为我在onCompletion()函数中创建了一个新的MediaPlayer实例,因此侦听器将不会与新创建的MediaPlayer实例相关联。因此,它不会侦听新创建的MediaPlayer的完成。我从setOnCompleteListener()函数中删除了新的MediaPlayer代码部分,现在可运行线程存在一些问题,这些线程一直在跟踪搜索栏和时间进度,从而导致它在到达文件末尾时崩溃

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
                if (chosenPath == null) {
                    seekBar.setProgress(0);
                    mediaPlayer.seekTo(0);
                } else {
                    songOrderCounter += 1;
                    System.out.println("SONG_ITERATOR_COUNT: " + songOrderCounter);
                    try {
                        /*handler.removeCallbacks(moveSeekBarThread);
                        songUpdateTimeHandler.removeCallbacks(updateSongTime);*/
                        Log.d("PATH_WORKING", allMusicFiles[songOrderCounter].toString());
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setDataSource(allMusicFiles[songOrderCounter].toString());
                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mediaPlayer.start();
                                metaRetriever();
                                seekBar.setProgress(0);
                            seekBar.setMax(mediaPlayer.getDuration());
                            trackTime();
                            }
                        });
                        mediaPlayer.prepareAsync();
                        metaRetriever(); /* Moved inside onPrepared*/


                    } catch (Exception e) {
                        System.out.println("NEXT_SONG_FAILED: " + e);
                    }
                }
            }
        });

以及导致崩溃的Runnable:

D/PATH_WORKING: /storage/emulated/0/Music/01 Control.mp3
I/System.out: NEXT_SONG_FAILED: java.lang.IllegalStateException
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.macyg.androidmediaplayer, PID: 12784
    java.lang.IllegalStateException
        at android.media.MediaPlayer.getCurrentPosition(Native Method)
        at com.example.macyg.androidmediaplayer.MainActivity$12.run(MainActivity.java:596)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

0 个答案:

没有答案