我尝试做的是添加1个按钮,播放和停止音乐。是否有可能像第一次点击播放一样循环它们然后第二次点击停止第三次点击将从开始和第四次停止再次开始等等?
基于我在google上的搜索,它是这样的?抱歉,如果没有问题
public void playtd(View view) {
if(!td.isPlaying()) {
td.start();
}
else if(td.isPlaying()) {
td.pause();
}
}
答案 0 :(得分:0)
试试这个:
MediaPlayer td;
boolean isPrepared;
public void prepareMediaPlayer(Uri yourTrackUri){
// call this on initialization of your mediaplayer
sMRMediaPlayer = MediaPlayer.create(mContext, uri);
sPrepared = true;
}
public void playtd(View view) {
if (!isPrepared) {
prepareMediaPlayer(Uri yourTrackUri)
}
if(!td.isPlaying()) {
td.start();
else {
// reprepare mediaplayer
td = MediaPlayer.create(Context, uriToUrFile);
isPrepared = true;
}
} else { // stop playing instead of pause, stop will start playing next time u play from beginning
td.stop();
}
void releaseMediaPlayer() {
if (td!= null) {
Log.d(TAG, "MediaPlayer is released.");
td.reset();
td.release();
td= null;
isPrepared = false;
}
}
调用releaseMediaPlayer一旦完成所有播放和停止的东西(可能在销毁等) 通过将跟踪uri传递给prepareMediaPlayer来初始化mediaplayer 在需要的地方添加try catch