播放歌曲时,MediaController播放按钮不会更新

时间:2014-09-05 18:33:36

标签: android android-mediaplayer

我正在尝试使用mediacontroller播放音频。我能够让音频工作但是当歌曲开始播放时我的mediacontroller上的播放按钮不会更新。如果按下mediacontroller上的播放按钮,它会暂停歌曲并寻找进度条。然后,如果我按暂停,它会再次播放该歌曲。我在打电话

mp.start() 

这不会更新播放按钮。当我打电话给start时,如何让按钮更改为暂停按钮?

public class QuickMusicPlayer implements MediaController.MediaPlayerControl {

private final MediaPlayer mMediaPlayer;
private final MediaController mMediaController;
private Activity mActivity;

public QuickMusicPlayer(View anchorView, Activity activity) {
    mActivity = activity;
    mMediaPlayer = new MediaPlayer();
    mMediaController = new MediaController(activity);
    mMediaController.setMediaPlayer(this);
    mMediaController.setAnchorView(anchorView);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}

public void setUrl(String url) {
    mMediaPlayer.reset();
    if (TextUtils.isEmpty(url)) {
        ViewUtil.showCroutonAlert(mActivity, R.string.cant_play_music);
    } else {
        try {

            mMediaPlayer.setDataSource(url);
            mMediaPlayer.prepareAsync();
            mMediaController.setEnabled(true);
            mMediaController.show(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void start() {
    mMediaPlayer.start();
}

@Override
public void pause() {
    mMediaPlayer.pause();
}

@Override
public int getDuration() {
    return mMediaPlayer.getDuration();
}

@Override
public int getCurrentPosition() {
    return mMediaPlayer.getCurrentPosition();
}

@Override
public void seekTo(int pos) {
    mMediaPlayer.seekTo(pos);
}

@Override
public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}

@Override
public int getBufferPercentage() {
    return 0;
}

@Override
public boolean canPause() {
    return true;
}

@Override
public boolean canSeekBackward() {
    return true;
}

@Override
public boolean canSeekForward() {
    return true;
}

@Override
public int getAudioSessionId() {
    return 0;
}

}

1 个答案:

答案 0 :(得分:7)

想出来。我需要在开始更新按钮后再次调用show

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