// #ifdef H5
const innerAudioContext = uni.createInnerAudioContext();
// #endif
// #ifndef H5
const innerAudioContext = uni.getBackgroundAudioManager()
// #endif
import $util from '@/util/util';
export default {
name:"audioplay",
data() {
return {
play:false,
h:uni.getSystemInfoSync().windowHeight,
showmore:false,
isplay:false,//是否已经播放了
index:0,//当前播放索引
};
},
props:{
list:{
type:Object,
default:[]
}
},
watch:{
list:{
handler(e) {
if(e.length >0){
//innerAudioContext.stop();
this.toplay(-1)
}
},
immediate: true,
}
},
created() {
this.onlisen()
},
methods:{
showlist(){
this.showmore = true;
},
hidelist(){
this.showmore = false;
},
prev(){
if(this.index <= 0){
$util.toast('已到第一首')
return
}
let i = this.index -1
let url = this.list[i].musicfile;
innerAudioContext.src = this.getimg(url);
this.index = i;
// #ifdef APP
innerAudioContext.title = this.list[i].title;
// #endif
},
next(){
if(this.index >= (this.list.length -1)){
$util.toast('已到最后一首')
return
}
let i = this.index +1
let url = this.list[i].musicfile;
innerAudioContext.src = this.getimg(url);
this.index = i;
// #ifdef APP
innerAudioContext.title = this.list[i].title;
// #endif
},
pause(){
},
toplay(i){
console.log(i)
if(i == -1){
if(!this.isplay){
let url = this.list[this.index].musicfile;
innerAudioContext.src = this.getimg(url);
return
}
if(this.play){
innerAudioContext.pause();
this.play = false
return
}
this.play = true
innerAudioContext.play();
return
}
this.index = i;
let url = this.list[i].musicfile;
innerAudioContext.src = this.getimg(url);
},
getimg(path){
return $util.get_img(path)
},
//可以开始播放事件
onlisen(){
let _ = this
// #ifdef APP
innerAudioContext.onPrev(()=>{
_.prev()
})
innerAudioContext.onNext(()=>{
_.next()
})
// #endif
//可以开始播放事件
innerAudioContext.onCanplay(()=>{
innerAudioContext.play();
_.showmore = false
_.isplay = true
_.play = true
})
//播放完成事件
innerAudioContext.onEnded((res1) => {
console.log('onEnded',res1);
if(this.index == this.list.length-1){
innerAudioContext.stop();
return
}
this.index ++;
let url = this.list[this.index].musicfile;
innerAudioContext.src = this.getimg(url);
});
//播放进度更新
innerAudioContext.onTimeUpdate(function(e){
console.log(JSON.stringify(e))
})
},
test(e){
}
}
}
4 个回复
接修复及原生单 (作者)
????
接修复及原生单 (作者)
接修复及原生单 (作者)
有人能回复下吗?
DCloud_UNI_Anne
是App端吗?用最新HBuilderX3.8.12 hello uni-app示例测试看下是否正常呢?
接修复及原生单 (作者)
我就是最新的3.8.12,我看到这个问题好久就有人提出来了 21年就提出来了
我直接复制demo进去就不行,难道nvue不行?
2023-09-04 18:41