import {
HYEventStore
} from "hy-event-store"
export const audioContext = wx.getBackgroundAudioManager();
const playerStore = new HYEventStore({
state: {
id: 0,
resetBgAudio: false,
currentSong: {},
durationTime: 0,
currentTime: 0,
name: '',
introduction: '',
image: '',
url: '',
lyricInfos: [],
currentLyricIndex: 0,
currentLyricText: "",
isFirstPlay: true,
playListSongs: [],
playListIndex: 0,
isStop: false,
playModeIndex: 0, // 0: 顺序播放 2:单曲循环 3:随机播放
isPlaying: false
},
actions: {
playMusicWithSongId(ctx, id, name, url, introduction, image) {
// 1.保存当前的id
ctx.id = id
ctx.name = name
ctx.url = url
ctx.introduction = introduction
ctx.image = image
console.log("isPlaying", id);
if (ctx.currentSong.id !== id) {
ctx.currentSong = {}
}
const system = wx.getSystemInfoSync()
let syt = system.system.replace(/[^a-z]+/ig, '')
console.log("syt", syt);
if (syt == 'iOS') {
audioContext.src = url
audioContext.autoplay = true
ctx.isPlaying = true
audioContext.title = ctx.name
} else {
wx.downloadFile({
url: url+`id=${id}`, // 仅为示例,实际使用时替换为你的音频文件下载链接
success: function (res) {
console.log("下载是否成功", res);
if (res.statusCode === 200) {
// 下载成功,res.tempFilePath 为临时文件路径
const tempFilePath = res.tempFilePath;
audioContext.src = tempFilePath
// audioContext.autoplay = true
audioContext.title = ctx.name
audioContext.play()
ctx.isPlaying = true
} else {
console.error('下载文件失败,状态码:', res.statusCode);
}
},
fail: function (err) {
console.error('下载文件失败:', err);
}
});
}
audioContext.onPlay(() => {
console.log('开始播放');
ctx.isPlaying = true
ctx.resetBgAudio = false
});
audioContext.onError((res) => {
console.log("播放不了", res.errMsg);
console.log("播放不了", res.errCode);
});
// 4.监听一些事件
if (ctx.isFirstPlay) {
ctx.isFirstPlay = false
this.dispatch("onAudioContextListener")
}
},
onAudioContextListener(ctx) {
audioContext.onTimeUpdate(() => {
let duration = audioContext.duration * 1000;
ctx.durationTime = duration;
// console.log("歌曲 时长", duration);
ctx.currentTime = audioContext.currentTime * 1000
})
audioContext.onWaiting(() => {
console.log("audioContext.onWaiting", ctx.isPlaying)
audioContext.pause()
})
audioContext.onPause(() => {
console.log("audioContext.onPause", ctx.isPlaying)
if (ctx.isPlaying) {
audioContext.pause()
ctx.isPlaying = false
}
})
audioContext.onCanplay(() => {
console.log("audioContext.onCanplay", ctx.isPlaying)
if (ctx.isPlaying) {
audioContext.play()
}
})
audioContext.onEnded(() => {
console.log("audioContext.onEnded")
this.dispatch("changeNewSongAction")
})
audioContext.onStop(function (res) {
// audioContext.pause()
ctx.isStop = true
ctx.isPlaying = false
ctx.resetBgAudio = true
console.log('onStop 开始监听', res)
})
},
changeNewSongAction(ctx, isNext = true) {
let index = ctx.playListIndex
let length = ctx.playListSongs.length
switch (ctx.playModeIndex) {
case 0: // 顺序播放
index = isNext ? index + 1 : index - 1
if (index === -1) index = length - 1
if (index === length) index = 0
break
case 1: // 单曲循环
break
case 2:
index = Math.floor(Math.random() * length)
break
}
ctx.playListIndex = index
console.log("歌曲", ctx.playListSongs);
// debugger
// 播放新歌曲
const id = ctx.playListSongs[index].id
const title = ctx.playListSongs[index].name
const url = ctx.playListSongs[index].url
const introduction = ctx.playListSongs[index].introduction
const image = ctx.playListSongs[index].image
if (id) {
this.dispatch("playMusicWithSongId", id, title, url, introduction, image)
} else {
return
}
},
changePlayStatusAction(ctx) {
console.log("是暂停还是停止", ctx.url, audioContext.paused);
if (audioContext.paused) {
if (ctx.resetBgAudio) {
audioContext.src = ctx.url
audioContext.title = ctx.name
}
audioContext.play()
ctx.isPlaying = true
} else {
audioContext.pause()
ctx.isPlaying = false
}
},
}
})
export default playerStore
手机型号,华为手机,型号COL-AL10,微信版本3.4.5,代码片段如上