我有这种方法可以用按钮播放音频文件:
final MediaPlayer mediaplayer = MediaPlayer.create(
this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.inro));
mediaplayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
} else {
mediaplayer.start();
}
}
});
我想要做的是,当音频正在播放并且用户再次单击该按钮时,音频将暂停,但当他再次单击它时,音频将恢复。
更新
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_nav_back:
WWHApplication.getMapActivityIntance().getVideoLocationDB();
WWHApplication.getMapActivityIntance().initLocations();
if (category == 'f') {
if (WWHApplication.getFavoritesListActivityIntance() != null)
WWHApplication.getFavoritesListActivityIntance()
.loadListView();
}
finish();
break;
case R.id.btn_tool_audio:
final MediaPlayer mediaplayer = MediaPlayer.create(this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.inro));
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
} else {
mediaplayer.start();
}
break;