简体   繁体   English

我想在媒体播放器中自动播放下一首歌

[英]I want to play next song automatically in media player

When I click on any song from playlist first time it plays next song, this problem has only happened for the first time on second or more click on any song from the playlist it working fine. 当我第一次单击播放列表中的任何歌曲时,第一次播放下一首歌曲时,仅在第二次或更多次单击播放列表中的任何歌曲时,才第一次出现此问题,它运行正常。

But the major problem has, it unable to play next song after the end of any song anytime. 但是主要的问题是,它无法在任何歌曲结束后随时播放下一首歌曲。

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.reset();
                // play next song
                if(listView_click_arg2 < (songPath.length - 1)){
                    listView_click_arg2=listView_click_arg2+1;
                }
                else{
                    // play first song
                    listView_click_arg2=0;
                }
            try {
                playSong(songPath[listView_click_arg2]);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });


private void playSong(String path) throws IllegalArgumentException, IllegalStateException, IOException {
    startTime=0;
    finalTime=0;
    oneTimeOnly=0;
    mediaPlayer.stop();
    mediaPlayer=null;
    mediaPlayer=new MediaPlayer();
    mediaPlayer.setDataSource(path);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

I found you have a problem because of mediaPlayer.reset() function. 我发现您由于mediaPlayer.reset()函数而遇到问题。 It says 它说

When a MediaPlayer object is just created using new or after reset() is called, it is in the Idle state; 当刚使用new创建MediaPlayer对象时,或者在调用reset()之后,它处于Idle状态。 and after release() is called, it is in the End state. 在调用release()之后,它处于End状态。 Between these two states is the life cycle of the MediaPlayer object. 在这两种状态之间是MediaPlayer对象的生命周期。

Please go through this link. 请通过此链接。 There is a nice Flow chart: https://developer.android.com/reference/android/media/MediaPlayer.html 有一个不错的流程图: https : //developer.android.com/reference/android/media/MediaPlayer.html

在此处输入图片说明

I belive, after you remove mediaPlayer.reset() inside the onCompletionListener body, it will get solved. 我相信,在onCompletionListener主体中删除mediaPlayer.reset()之后,它将得到解决。

Try adding release(). 尝试添加release()。 It looks like a out of memory issue. 看来是内存不足的问题。

private void playSong(String path) throws IllegalArgumentException, IllegalStateException, IOException {
    startTime=0;
    finalTime=0;
    oneTimeOnly=0;
    // changing the state of mediaPlayer inside its own callback is a bad practice.
    //mediaPlayer.stop();
    mediaPlayer.release(); // <<------ Add this before reference is gone.
    mediaPlayer=null;
    mediaPlayer=new MediaPlayer();
    mediaPlayer.setDataSource(path);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM