不支持流式加载wav和flac, 需要全部加载完毕才能播放

This commit is contained in:
2023-10-04 13:07:00 +08:00
parent d973619718
commit a56c81c419
1 changed files with 48 additions and 42 deletions

View File

@ -140,13 +140,18 @@ export default class MusicList {
} }
async play(item) { async play(item) {
if (!item.arrayBuffer) { if (!item.arrayBuffer) {
// 不支持流式加载wav和flac, 需要全部加载完毕才能播放
if (item.type === 'audio/wav' || item.type === 'audio/flac') {
await this.load(item)
this.audio.src = URL.createObjectURL(new Blob([item.arrayBuffer], { type: item.type }))
this.audio.play()
} else {
// 边加载边播放 // 边加载边播放
const mediaSource = new MediaSource() const mediaSource = new MediaSource()
this.audio.src = URL.createObjectURL(mediaSource) this.audio.src = URL.createObjectURL(mediaSource)
if (!item.arrayBufferChunks) item.arrayBufferChunks = [] if (!item.arrayBufferChunks) item.arrayBufferChunks = []
mediaSource.addEventListener('sourceopen', async () => { mediaSource.addEventListener('sourceopen', async () => {
const type = item.type === 'audio/wav' ? 'audio/wav; codecs=1' : item.type const sourceBuffer = mediaSource.addSourceBuffer(item.type)
const sourceBuffer = mediaSource.addSourceBuffer(type)
const arrayBufferLoader = async (index = 0) => { const arrayBufferLoader = async (index = 0) => {
console.log('开始加载====================================') console.log('开始加载====================================')
// 按照数据长度计算出分片应有数量, 如果数量不到且没有停止加载则一直读取 // 按照数据长度计算出分片应有数量, 如果数量不到且没有停止加载则一直读取
@ -183,6 +188,7 @@ export default class MusicList {
this.audio.play() this.audio.play()
arrayBufferLoader() arrayBufferLoader()
}) })
}
} else { } else {
// 本地缓存直接播放 // 本地缓存直接播放
this.audio.src = URL.createObjectURL(new Blob([item.arrayBuffer], { type: item.type })) this.audio.src = URL.createObjectURL(new Blob([item.arrayBuffer], { type: item.type }))