70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
/* #ifdef H5 */
|
|
const bgAudioManager = uni.createInnerAudioContext();
|
|
/* #endif */
|
|
/* #ifdef APP-PLUS */
|
|
const bgAudioManager = uni.getBackgroundAudioManager();
|
|
/* #endif */
|
|
bgAudioManager.autoplay = false;
|
|
|
|
|
|
function initPlayer(that){
|
|
if(that.currentPlaySong){
|
|
that.$u.vuex("currentPlayer",{
|
|
playState:false,
|
|
progress:0,
|
|
musicId:that.currentPlaySong.id,
|
|
});
|
|
bgAudioManager.title = that.currentPlaySong.songName;
|
|
bgAudioManager.singer = that.currentPlaySong.artist;
|
|
bgAudioManager.coverImgUrl = that.currentPlaySong.pic;
|
|
bgAudioManager.src = that.currentPlaySong.playSrc;
|
|
console.log("播放器初始化完成");
|
|
bindOnControllerPlay(that);
|
|
}
|
|
}
|
|
|
|
function bindOnControllerPlay(that){
|
|
let playState=that.currentPlayer.playState;
|
|
console.log("that.currentPlayer.progress",that.currentPlayer.progress);
|
|
if(playState){
|
|
bgAudioManager.pause();
|
|
}else{
|
|
if(that.currentPlayer.canPlay==false){
|
|
initPlayer(that);
|
|
return;
|
|
}else{
|
|
bgAudioManager.play();
|
|
that.$u.vuex("currentPlayer.canPlay",true);
|
|
}
|
|
}
|
|
playState=!playState;
|
|
console.log("当前播放状态===",playState);
|
|
that.$u.vuex("currentPlayer.playState",playState);
|
|
that.$u.vuex("currentPlayer.canPlay",true);
|
|
bgAudioManager.onTimeUpdate((e) => {
|
|
let progress = (bgAudioManager.currentTime / bgAudioManager.duration) * 100;
|
|
if (progress) {
|
|
that.$u.vuex("currentPlayer.progress",progress);
|
|
}
|
|
});
|
|
bgAudioManager.onError(()=>{
|
|
resetPlayer(that);
|
|
});
|
|
bgAudioManager.onEnded(() => {
|
|
resetPlayer(that);
|
|
});
|
|
}
|
|
|
|
function resetPlayer(that,player){
|
|
bgAudioManager.stop();
|
|
that.$u.vuex("currentPlayer.playState",false);
|
|
that.$u.vuex("currentPlayer.progress",0);
|
|
that.$u.vuex("currentPlayer.canPlay",false);
|
|
}
|
|
|
|
module.exports = {
|
|
initPlayer,
|
|
bindOnControllerPlay,
|
|
resetPlayer,
|
|
bgAudioManager
|
|
} |