我正在制作播放存储在firebase上的mp3混音的媒体播放器。我可以使链接没有问题。但当我再次按下该项目时,我希望它相当于按下停止。但由于某种原因,它并没有阻止它启动媒体的新实例。有人可以告诉我,我做错了。
我的代码
在我的创建
Array#reduce
然后我的方法
let dft = ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"];
let ans = [4, 6, 12, -1];
let res = dft.reduce((s,a,i) => {
(ans.indexOf(i) > -1) ? s.push(a, '-') : s.push(a);
return s;
}, []);
console.log(res);
然后在项目onclick
mMediaplayer = null;
答案 0 :(得分:2)
问题:假设媒体播放器已经在播放
// song is playing
mMediaplayer = new MediaPlayer(); // you created a new player
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
if (mMediaplayer.isPlaying()) { // new player is not in playing state
stopPlaying(); // so you always checking the state of new player
} else {
playMedia();
}
先检查然后创建
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
stopPlaying();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
playMedia();
所以逻辑可以简化为
if (mMediaplayer!=null && mMediaplayer.isPlaying()) {
mMediaplayer.stop();
mMediaplayer.release();
}
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(mp3);
mMediaplayer.prepare();//prepare to play
mMediaplayer.start();