Files
nuttyreading-html/utils/music.js
2023-08-11 14:54:03 +08:00

292 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import store from '@/store/index.js'
import $http from '@/config/requestConfig.js';
const bgm = uni.getBackgroundAudioManager();
// const bgm = uni.createInnerAudioContext();
bgm.musicList = [] // 播放目录
bgm.src = ''
bgm.cTime = 0
bgm.title = ''
bgm.loop = true;
bgm.coverImgUrl = ''
bgm.oldIndex = 0 // 前面一首的播放索引
bgm.playIndex = 0
bgm.playing = false // 播放进行时
var music = {
//
//mute 表示是否是播放,,默认不播放
playBgm({mute=false}){
// console.log(bgm.musicList,'src')
if (!bgm) return;
if(mute){
bgm.pause()
}else{
// bgm.src = bgm.musicList[bgm.playIndex].url
// 判断播放列表是否空
if(bgm.musicList.length == 0){
uni.showModal({
title: '提示',
showCancel:false,
confirmText:'好的',
content: '请先添加音频到播放列表,再点播放键开始听书',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
return
}
// 没有就添加添加url到播放器播放新的
if(bgm.src == ''){
store.state.userInfo.playIndex !== undefined ? bgm.playIndex = store.state.userInfo.playIndex : bgm.playIndex = 0
// console.log(bgm.playIndex,'初始化播放index');
this.getChartUrl()
}else{
// 如果有正在播放的url暂停改为播放
// bgm.play()
}
}
bgm.onPause(()=>{
console.log('暂停背景音乐');
store.commit('setUserInfo',{'playFlag': false})
})
bgm.onPlay(() => {
store.commit('setUserInfo',{'playFlag': true})
console.log('开始播放音乐#######');
})
bgm.onError((res) => {
console.log(res)
uni.showModal({
title: '提示',
showCancel:false,
confirmText:'好的',
content: '音频路径异常,请联系管理员',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
});
return
})
bgm.onTimeUpdate((res) =>{
// this.currentTime = Math.max(0, this.videoContext.currentTime)
setTimeout(()=>{
store.commit('setUserInfo',{'currentTime': Math.max(0, bgm.currentTime)})
},1000)
})
bgm.onWaiting(() => {
// 加载时
uni.showLoading()
console.log('加载时')
})
bgm.onCanplay(() => {
// 可以播放时
uni.hideLoading()
this.saveIndex()
store.commit('setUserInfo',{'duration':bgm.duration})
store.commit('setUserInfo',{'playTitle': bgm.title})
store.commit('setUserInfo',{'fengImg': bgm.coverImgUrl})
store.commit('setUserInfo',{'playFlag': true})
store.commit('setUserInfo',{'playingInfo': bgm.musicList[bgm.playIndex]})
})
bgm.onEnded(() => {
console.log('播放完毕了',bgm.playing)
store.commit('setUserInfo',{'playFlag': false})
this.setPlayIndex('next') // 下一首
})
},
// 添加播放列表
setList(list,op){
bgm.musicList = list
bgm.playIndex = 0
store.commit('setUserInfo',{'playingInfo': {}})
if(op == 'autoPlay'){
if(bgm._options.src == ''){
// this.getChartUrl()
this.playBgm({mute:false})
console.log('playBgm')
}else{
this.getChartUrl() // 获取对应的播放路径
// console.log('getChartUrl')
}
}
},
// 获取章节url
getChartUrl(){
// bgm.stop()
let data = {
'userId': store.state.userInfo.id,
'bookid': bgm.musicList[bgm.playIndex].bookid,
'chapterid': bgm.musicList[bgm.playIndex].chapterId
}
// console.log(data,'data')
$http.post('book/bookchaptercontent/appBooksChapterContent', data)
.then(res => {
console.log(res,'res')
if(res.code == 0){
// bgm.stop()
// 有听权限时
if(res.bookCatalogue[0].url.length > 10){
bgm.src = res.bookCatalogue[0].url
bgm.title = res.bookCatalogue[0].name
bgm.coverImgUrl = res.image
// bgm.pause()
// this.setCoverImg(bgm.musicList[bgm.playIndex].images)
}else{
bgm.src = ''
bgm.title = ''
bgm.coverImgUrl = ''
// this.setCoverImg('../../static/icon/x1.jpg')
// bgm.pause()
uni.showToast({
title:'音频地址异常',
icon:'error',
duration:2000
})
// store.commit('setUserInfo',{'playFlag': false})
}
store.commit('setUserInfo',{'playTitle': bgm.title})
store.commit('setUserInfo',{'fengImg': bgm.coverImgUrl})
}
console.log(res.bookCatalogue,'单章节信息')
}).catch((e)=>{
// bgm.pause()
bgm.playIndex = bgm.oldIndex
bgm.stop()
store.commit('setUserInfo',{'playFlag': false})
uni.showModal({
title: '提示',
cancelText: '暂不购买',
confirmText:'立即购买',
content: '抱歉!当前章节不支持试听,购买后可收听',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
});
})
},
// 存储播放index
saveIndex(){
store.commit('setUserInfo',{'playIndex': bgm.playIndex})
},
// 存储播放
saveTimes(){
store.commit('setUserInfo',{'playTimes': bgm.currentTime})
},
// 设置播放index
setPlayIndex(opName){ // 切换音频
if(opName == 'next'){ // 下一首
if(bgm.playIndex == bgm.musicList.length - 1){
uni.showToast({
title:'列表播放完毕~~',
icon: 'none'
})
return
}
bgm.oldIndex = bgm.playIndex
bgm.playIndex += 1
console.log('下一首',bgm.playIndex,bgm.oldIndex, store.state.userInfo.playIndex)
// bgm.stop()
this.getChartUrl() // 获取章节url
//this.playBgm({'mute':false})
// store.commit('setUserInfo',{'playFlag': false})
}else{ // 上一首
if(bgm.playIndex == 0){
uni.showToast({
title:'没有上一首了~',
icon: 'none'
})
return
}
bgm.oldIndex = bgm.playIndex
bgm.playIndex -= 1
console.log('上一首',bgm.playIndex)
this.getChartUrl() // 获取章节url
}
},
// 设置封面图片
setCoverImg(url){
// console.log(bgm.coverImgUrl,'bgm.coverImgUrl')
bgm.coverImgUrl = url
},
// 获取封面图片
getCoverImg(){
// console.log(bgm.coverImgUrl,'bgm.coverImgUrl')
return bgm.coverImgUrl
},
// 获取播放列表
// getLibList(){
// return bgm.musicList
// },
// 播放单曲
setOneMusic(item){
console.log(item,'item')
// 显示播放组件
store.commit('setUserInfo',{'playVisible': true})
uni.setStorage({
key: 'playVisible',
data: true,
success: function () {
console.log('success');
}
});
// 先检查列表里是否存在item避免重复添加
// 存在就不添加找到对应的index,通过参数获得url
// 不存在就添加进列表,直接切换下一章节
var has = false
for (var i = 0; i < bgm.musicList.length; i++) {
if(item.chapterId == bgm.musicList[i].chapterId && item.bookid == bgm.musicList[i].bookid){
has = true
break
}
}
if(has){
bgm.playIndex = bgm.musicList.findIndex(function(info){
// console.log(info,'info')
if(item.chapterId == info.chapterId && item.bookid == info.bookid ){
return info;
}
})
console.log(bgm.playIndex,'bgm.playIndex')
this.getChartUrl()
}else{
bgm.musicList.push(item) // 添加到播放目录
var newVal ={ // 本地播放数据更新
'myList' : bgm.musicList,
}
uni.setStorage({
key: 'playData',
data: newVal,
success: function () {
console.log('success');
}
});
bgm.playIndex = bgm.musicList.length - 2
console.log(bgm.playIndex,'bgm.playIndex')
// this.getChartUrl()
this.setPlayIndex('next')
}
},
// 关闭音频
setCloseBgm(){
bgm.stop()
}
}
module.exports = {music,bgm}