mediaPlayer CompletionListener 在完成歌曲之前播放下一首歌曲

mediaPlayer CompletionListener playing next song before Completion of song

我已经编写了一个 mediaPlayer 应用程序,其中 mediaplayer.setOnCompletionListener 在歌曲完成之前被调用,每首歌曲都会继续...它会继续播放下一首歌曲,直到到达最后一首歌曲播放列表...请帮忙

这是我的代码:

 tracksListViewForSpeakers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Track track = playlistContents.get(position);
                String filepath = track.getFilePath();
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(filepath);
                    mediaPlayer.start();

//                    mediaPlayer.setVolume(0, 0);
                } catch (IOException e) {
                    e.printStackTrace();
                    for(int i=0;i<4;i++)
                        System.out.println("MediaPlayer Exception");
                }

            }
        });

完成侦听器

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                onNextClicked(mp);
            }
        });

OnNextClickedMethod

public void onNextClicked(MediaPlayer mp){
int val = myModel.getCurrentPlayingIndex();
val=val+1;
Track track = currentPlayList.get(val);
        String filepath = track.getFilePath();
if (mp.isPlaying()) {
            mp.stop();
        }
        mp.reset();
        try {
            mp.setDataSource(filepath);
            mp.start();
//            mediaPlayer.setVolume(0, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }

  }

}

这样试试

//set path
mediaPlayer.setDataSource("yourFilePath");
mediaPlayer.setOnPrepareListener(new OnPrepareListener(MediaPlayer mediaPlayer) {
       mediaPlayer.start();
});
mediaPlayer.prepareAsync();

没有prepareAsync();就不要打电话给start() 希望能解决你的问题..

完成后使用此播放下一首歌曲

@Override
    public void onCompletion(MediaPlayer arg0) {

        if (isRepeat) {
            // repeat is on play same song again
            playSong(currentSongIndex);
        } else if (isShuffle) {
            // shuffle is on - play a random song
            Random rand = new Random();
            currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
            playSong(currentSongIndex);
        } else {
            // no repeat or shuffle ON - play next song
            if (currentSongIndex < (songsList.size() - 1)) {
                playSong(currentSongIndex + 1);
                currentSongIndex = currentSongIndex + 1;
            } else {
                // play first song
                playSong(0);
                currentSongIndex = 0;
            }
        }

    }

那么播放歌曲的方法就是这样

public void  playSong(int songIndex){
        // Play song
        try {
            mp.reset();
            mp.setDataSource(songsList.get(songIndex).get("songPath"));
            mp.prepare();
            mp.start();
            // Displaying Song title
            String songTitle = songsList.get(songIndex).get("songTitle");
            songTitleLabel.setText(songTitle);

            // Changing Button Image to pause image
            btnPlay.setImageResource(R.drawable.btn_pause);

            // set Progress bar values
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);

            // Updating progress bar
            updateProgressBar();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

或者你可以看到这个教程并实现这个

http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/