0
我想在活動首次啓動時立即開始播放聲音,並希望MediaPlayerControl
對象反映此更改。在下面給出的代碼中,控件將從播放模式開始,而不是暫停模式(因爲文件已經播放)。立即啓動MediaPlayer並更新MediaPlayerControl
public class Guide extends AppCompatActivity implements MediaPlayerControl, MediaPlayer.OnPreparedListener {
private MediaPlayer m_audio_player;
private MediaController m_audio_controller;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
set_up_audio();
}
private void set_up_audio(){
m_audio_player = MediaPlayer.create(this, *SOMESOUNDFILE*);
m_audio_player.setOnPreparedListener(this);
m_audio_controller = new MediaController(this);
}
public void onPrepared(MediaPlayer mediaPlayer) {
m_audio_controller.setMediaPlayer(this);
m_audio_controller.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
m_audio_controller.setEnabled(true);
m_audio_controller.show();
m_audio_player.start();
// how do I update m_audio_controller's state here?
}
});
}
}
我該怎麼做?