import Vue from 'vuex'
import {addHistory} from '../../common/network/api.js'
import {countDownTime} from '@/common/util/countDownTime.js'
export default{
state:{
player:null,
currentTime:0,
paused:true,
buffered:0,
duration:0,
autoplay:false,
startTime:0,
audioList:[],
currentIndex:-1,
currentAudioItem:{},
playMode:0,//0顺序播放,1随机 2单曲循环
timingDownTime:null,
cancelTimingId:null
},
mutations:{
playerInit(state){
// let player=uni.createInnerAudioContext();
let player = uni.getBackgroundAudioManager();
state.player=player;
let self = this;
player.onCanplay(function(){
console.log("音频可以播放",player.duration);
state.duration=player.duration;
try {
let log = {
event: 'listen',
content_name:state.currentAudioItem.name,
content_id:state.currentAudioItem.id,
type:10
};
self.dispatch('SET_APP_LOG', log);
} catch (e) {
//TODO handle the exception
}
})
player.onPlay(function(){
console.log("音频播放")
state.paused=player.paused;
})
player.onPause(function(){
state.paused=player.paused;
})
player.onError(function(error){
console.log("播放错误",error);
state.paused=player.paused;
state.duration = 0;
try {
let log = {
event: 'audio_invalid',
content_name:state.currentAudioItem.name,
content_id:state.currentAudioItem.id,
url:state.currentAudioItem.url
};
self.dispatch('SET_APP_LOG', log);
} catch (e) {
//TODO handle the exception
}
})
player.onEnded(function(){
console.log("音频自然播放结束")
self.commit("playEndNext");
})
player.onWaiting(function(){
console.log("正在加载")
})
player.onTimeUpdate(function(){
state.currentTime=player.currentTime
})
player.onNext(function(){
self.commit("playNext");
console.log("heelo")
})
player.onPrev(function(){
self.commit("playPre");
console.log("nextssss")
})
},
playAudioList(state,param){
state.audioList=param.audioList;
state.currentIndex=param.index;
if(state.player != null && state.audioList.length!=0){
this.commit("playIndex",state.currentIndex);
}
try{
uni.setStorageSync("playList",JSON.stringify(param.audioList));
}catch(e){
}
},
readyPlayAudioList(state,param){
state.audioList=param.audioList;
state.currentIndex=param.index;
let audioItem = state.audioList[param.index];
state.currentAudioItem=audioItem;
state.player.src=audioItem.url;
state.player.pause();
},
playEndNext(state){
if(state.playMode == 0){//顺序
this.commit("playNext")
}
if(state.playMode == 1){//随机
let audioCount = state.audioList.length;
let randomIndex = Math.floor(Math.random()*audioCount);
this.commit("playIndex",randomIndex);
}
if(state.playMode == 2){//单曲
this.commit("playIndex",state.currentIndex);
}
},
playNext(state){
if(state.playMode == 0 || state.playMode == 2){
let nextIndex = state.currentIndex+1;
if(nextIndex<state.audioList.length){
state.currentIndex=nextIndex;
}else{
nextIndex = 0;
state.currentIndex = 0;
}
this.commit("playIndex",nextIndex);
}
if(state.playMode == 1){
let audioCount = state.audioList.length;
let randomIndex = Math.floor(Math.random()*audioCount);
this.commit("playIndex",randomIndex);
}
},
playPre(state){
let preIndex = state.currentIndex-1;
if(preIndex<0){
state.currentIndex=state.audioList.length-1;
preIndex=state.audioList.length-1;
}else{
state.currentIndex = preIndex;
}
this.commit("playIndex",preIndex);
},
playorpause(state){
if(state.paused){
state.player.play();
}else{
state.player.pause();
}
},
playIndex(state,param){
let audioItem = state.audioList[param];
state.currentAudioItem=audioItem;
state.player.src=audioItem.url;
state.player.title = audioItem.name;
state.player.coverImgUrl=audioItem.coverImage;
state.player.play();
this.dispatch("addHistory",audioItem);
},
playerSeek(state,param){
state.player.seek(param);
},
playModeChange(state){
if(state.playMode == 0){
state.playMode =1;
state.player.loop == false;
uni.showToast({
title:'随机播放',
icon:'none'
})
}else if(state.playMode ==1){
state.playMode = 2;
state.player.loop == true;
uni.showToast({
title:'单曲循环',
icon:'none'
})
}else{
state.playMode = 0;
state.player.loop == false;
uni.showToast({
title:'顺序播放',
icon:'none'
})
}
},
playChangeShowTime(state,allTime){
if (allTime > 0) {
state.timingDownTime = countDownTime(allTime)
console.log('this.countTime',state.timingDownTime)
}
else if(allTime <= 0){
clearInterval(state.cancelTimingId)
state.timingDownTime = null
state.player.pause();
}
else {
clearInterval(state.cancelTimingId)
state.timingDownTime = null
}
},
playChangeCancelSet(state,cancelTimingId){
state.cancelTimingId = cancelTimingId
}
},
getters:{
currentTime_getter:state=>{
return parseInt(state.currentTime);
},
duration_getter:state=>{
return parseInt(state.duration);
}
},
actions:{
playerInit(context){
context.commit("playerInit")
},
playAudioList(context,param){
context.commit("playAudioList",param)
},
readyPlayAudioList(context,param){
context.commit("readyPlayAudioList",param)
},
playIndex(context,index){
context.commit("playIndex",index)
},
playNext(context){
context.commit("playNext");
},
playPre(context){
context.commit("playPre");
},
playorpause(context){
context.commit("playorpause");
},
playerSeek(context,param){
context.commit("playerSeek",param);
},
playModeChange(context){
context.commit("playModeChange");
},
addHistory({rootState},param){
if(rootState.user.isLogin == false){
return;
}
let hisparam = {
userId:rootState.user.userInfo.userId,
sourceId:param.id,
sourceType:10
}
addHistory(hisparam).then((res)=>{
console.log("添加历史记录",res)
},fail=>{
console.log("添加历史记录失败")
})
},
countDownTimeFun(context,allTime){
clearInterval(context.state.cancelTimingId)
let setIntervalId = setInterval(() => {
allTime = allTime - 1 * 1000
context.commit('playChangeShowTime',allTime)
}, 1000)
context.commit('playChangeCancelSet',setIntervalId)
}
}
}```
- 发布:2022-02-22 14:34
- 更新:2023-06-22 17:27
- 阅读:920
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: windows10 专业版
HBuilderX类型: 正式
HBuilderX版本号: 3.2.12
手机系统: Android
手机系统版本号: Android 10
手机厂商: 华为
手机机型: 华为p20小米6x华为mate40pro
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
App下载地址或H5⽹址: https://a.app.qq.com/o/simple.jsp?pkgname=com.booyue.babylisten
示例代码:
操作步骤:
复现操作
进入APP首页,点击随意专辑,进入播放页。点击播放,APP切换后台播放,手机息屏后等待。有时10分钟左右停止播放,APP进程中断。有时是40分钟左右。
复现操作
进入APP首页,点击随意专辑,进入播放页。点击播放,APP切换后台播放,手机息屏后等待。有时10分钟左右停止播放,APP进程中断。有时是40分钟左右。
预期结果:
预期结果
手机一直进行播放,或者出现特殊情况中断播放
预期结果
手机一直进行播放,或者出现特殊情况中断播放
实际结果:
复现操作
手机息屏后等待,有时10分钟左右停止播放,APP进程中断。有时是40分钟左右。
复现操作
手机息屏后等待,有时10分钟左右停止播放,APP进程中断。有时是40分钟左右。
bug描述:
安卓鸿蒙手机使用,场景是音频播放器,后台播放被中断,app进程被杀死。
使用的app中使用uni.getBackgroundAudioManager(),app进入后台,手机息屏后,播放一段时间,后台播放被中断,app进程被杀死。有时播放7-10分钟左右,问题出现。也有在40分钟左右出现问题
jianyu123 (作者)
暂时发现非iOS,都存在这个问题
2022-02-22 19:14