Android MediaPlayer不会播放不同的歌曲

时间:2014-03-06 05:40:24

标签: android android-mediaplayer

我正在尝试使用一个MediaPlayer连续播放多首歌曲。第一首歌将根据需要播放,但之后,一首特定歌曲(按字母顺序排列的第一首歌曲)将一遍又一遍地播放。我遵循了这个:Android Mediaplayer play different songs after eachother

public void startPlayer(View view) {
    game = new Game();
    TextView textView = (TextView) findViewById(R.id.title);

    // start first song
    Music firstSong = game.getNextSong();
    textView.setText(firstSong.getID());
    mediaPlayer = MediaPlayer.create(view.getContext(), firstSong.getID());

    // make sure rest of songs play
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    goToNextSong();
                }
            });

    // actually start!
    mediaPlayer.start();
}

public void goToNextSong() {
    Music nextSong = game.getNextSong();
    TextView textView = (TextView) findViewById(R.id.title);

    // if we still have music to play
    if (nextSong != null) {
        try {
            // set the new title
            textView.setText(nextSong.getID());
            mediaPlayer.stop();
            mediaPlayer.reset();

            // get the music file
             FileDescriptor fd = getResources().openRawResourceFd(
                    nextSong.getID()).getFileDescriptor();
            mediaPlayer.setDataSource(fd);

            // play it!
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
}

即使我将fd设置为特定歌曲,它仍会按字母顺序播放第一首歌曲。 nextSong.getID()返回R.raw.somesong。 textview(设置为歌曲ID)更改为正确的歌曲。帮助

1 个答案:

答案 0 :(得分:0)

所以我没有找到一种方法来保持相同的MediaPlayer并播放不同的歌曲,所以我每次只是制作一个新歌。它有效!

public void startPlayer() {
    game = new Game();
    goToNextSong();
}

public void goToNextSong() {
    Music nextSong = game.getNextSong();
    TextView textView = (TextView) findViewById(R.id.title);

    // if we still have music to play
    if (nextSong != null) {
        try {
            // set the new title
            textView.setText(nextSong.getID());

            // stop old music player
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }

            // create new music player
            mediaPlayer = MediaPlayer.create(textView.getContext(),
                    nextSong.getID());

            // make sure rest of songs play
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            goToNextSong();
                        }
                    });

            // actually start!
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // we're done!
        mediaPlayer.release();
        mediaPlayer = null;
    }
}
相关问题