在我的音乐播放器列表中查看歌曲项目在第二次单击时不起作用

时间:2016-10-11 20:13:36

标签: android android-mediaplayer

我使用线程来更新SeekBar。如果我不启动该线程,则不会出现此问题。我现在能做什么...?我只是遇到了项目这一部分的问题。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (mp != null) {
                mp.stop();
                mp.release();
                toast("Second Time");

            }

            u = Uri.parse(collectedSong.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            sb.setMax(mp.getDuration());

            **updateSeekBar.start();**

            positionNew = position;

        }
    });

1 个答案:

答案 0 :(得分:0)

查看Android文档中的MediaPlayer生命周期:

https://developer.android.com/reference/android/media/MediaPlayer.html

Android MediaPlayer lifecycle

这就是我使用媒体播放器播放流歌曲的方式:

public void onSongClicked(Song song) {
    //check if is playing
    stopMediaPlayer();

    // prepare media player to play sample
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });

    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
            mMediaPlayer = null;
        }
    });

    try {
        mMediaPlayer.setDataSource(song.getPath());
        mMediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void stopMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

我希望它有所帮助:)