|
|
@ -24,12 +24,16 @@ Page({ |
|
|
|
toastShow: false, |
|
|
|
toastShow: false, |
|
|
|
toastType: 3, |
|
|
|
toastType: 3, |
|
|
|
previewImg: true, |
|
|
|
previewImg: true, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
listener: false, |
|
|
|
|
|
|
|
globalStop: false, |
|
|
|
}, |
|
|
|
}, |
|
|
|
innerAudioContext: null as WechatMiniprogram.InnerAudioContext | null, |
|
|
|
BackgroundAudioManager: null as WechatMiniprogram.BackgroundAudioManager | null, |
|
|
|
onLoad(options) { |
|
|
|
onLoad(options) { |
|
|
|
this.innerAudioContext = wx.createInnerAudioContext(); |
|
|
|
this.BackgroundAudioManager = wx.getBackgroundAudioManager(); |
|
|
|
this.setData({ |
|
|
|
this.setData({ |
|
|
|
BookId: options.id, |
|
|
|
BookId: options.id, |
|
|
|
|
|
|
|
globalStop: this.BackgroundAudioManager.paused, |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (options.cid) { |
|
|
|
if (options.cid) { |
|
|
|
this.setData({ |
|
|
|
this.setData({ |
|
|
@ -46,12 +50,6 @@ Page({ |
|
|
|
this.getDetail(); |
|
|
|
this.getDetail(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
onUnload() { |
|
|
|
|
|
|
|
if (this.innerAudioContext) { |
|
|
|
|
|
|
|
this.innerAudioContext?.stop(); |
|
|
|
|
|
|
|
this.innerAudioContext?.destroy(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
getChapterList() { |
|
|
|
getChapterList() { |
|
|
|
wx.ajax({ |
|
|
|
wx.ajax({ |
|
|
|
method: 'GET', |
|
|
|
method: 'GET', |
|
|
@ -102,6 +100,7 @@ Page({ |
|
|
|
title: res.book.BookName, |
|
|
|
title: res.book.BookName, |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.handleView(); |
|
|
|
this.handleView(); |
|
|
|
|
|
|
|
this.initBackgroundAudioManager(); |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((err) => { |
|
|
|
.catch((err) => { |
|
|
|
if (err.data.code === 403) { |
|
|
|
if (err.data.code === 403) { |
|
|
@ -181,57 +180,107 @@ Page({ |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
initBackgroundAudioManager() { |
|
|
|
handlePlay() { |
|
|
|
const { chapter } = this.data; |
|
|
|
const { chapter, play, currentTime } = this.data; |
|
|
|
if (this.BackgroundAudioManager) { |
|
|
|
if (this.innerAudioContext) { |
|
|
|
if (this.BackgroundAudioManager.src === chapter.AudioUrl.url && !this.BackgroundAudioManager.paused) { |
|
|
|
if (play) { |
|
|
|
this.setData({ |
|
|
|
this.innerAudioContext.stop(); |
|
|
|
play: true, |
|
|
|
return; |
|
|
|
}); |
|
|
|
} |
|
|
|
this.backgroundAudioManagerListener(); |
|
|
|
this.innerAudioContext.stop(); |
|
|
|
} else { |
|
|
|
this.innerAudioContext.src = chapter.AudioUrl.url; |
|
|
|
this.backgroundAudioManagerListener(); |
|
|
|
this.innerAudioContext.seek(currentTime); |
|
|
|
|
|
|
|
this.innerAudioContext.play(); |
|
|
|
|
|
|
|
const listener = () => { |
|
|
|
|
|
|
|
if (this.innerAudioContext && !this.data.drag) { |
|
|
|
|
|
|
|
const { currentTime, duration } = this.innerAudioContext; |
|
|
|
|
|
|
|
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`; |
|
|
|
|
|
|
|
const progress = (currentTime / duration) * 100; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
time, |
|
|
|
|
|
|
|
progress, |
|
|
|
|
|
|
|
currentTime, |
|
|
|
|
|
|
|
play: true, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
this.innerAudioContext.onTimeUpdate(listener); |
|
|
|
|
|
|
|
this.innerAudioContext.onEnded(() => { |
|
|
|
|
|
|
|
this.innerAudioContext?.seek(0); |
|
|
|
|
|
|
|
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
this.setData({ |
|
|
|
play: false, |
|
|
|
|
|
|
|
progress: 0, |
|
|
|
progress: 0, |
|
|
|
time, |
|
|
|
|
|
|
|
currentTime: 0, |
|
|
|
currentTime: 0, |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
this.innerAudioContext.onPlay(() => { |
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
backgroundAudioManagerListener() { |
|
|
|
|
|
|
|
const { chapter } = this.data; |
|
|
|
|
|
|
|
if (!this.BackgroundAudioManager) return; |
|
|
|
|
|
|
|
const timeUpdateListener = () => { |
|
|
|
|
|
|
|
const isCurrent = this.BackgroundAudioManager?.src === chapter.AudioUrl.url; |
|
|
|
|
|
|
|
if (this.BackgroundAudioManager && !this.data.drag && this.data.play && isCurrent) { |
|
|
|
|
|
|
|
const { currentTime, duration } = this.BackgroundAudioManager; |
|
|
|
|
|
|
|
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`; |
|
|
|
|
|
|
|
const progress = (currentTime / duration) * 100; |
|
|
|
this.setData({ |
|
|
|
this.setData({ |
|
|
|
play: true, |
|
|
|
time, |
|
|
|
|
|
|
|
progress, |
|
|
|
|
|
|
|
currentTime, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
const endedListener = () => { |
|
|
|
|
|
|
|
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
play: false, |
|
|
|
|
|
|
|
progress: 0, |
|
|
|
|
|
|
|
time, |
|
|
|
|
|
|
|
currentTime: 0, |
|
|
|
|
|
|
|
globalStop: true, |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.innerAudioContext.onPause(() => { |
|
|
|
}; |
|
|
|
this.setData({ |
|
|
|
const playListener = () => { |
|
|
|
play: false, |
|
|
|
const isCurrent = this.BackgroundAudioManager?.src === chapter.AudioUrl.url; |
|
|
|
}); |
|
|
|
if (!isCurrent) return; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
play: true, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
const pauseListener = () => { |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
play: false, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
const stopListener = () => { |
|
|
|
|
|
|
|
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
play: false, |
|
|
|
|
|
|
|
globalStop: true, |
|
|
|
|
|
|
|
progress: 0, |
|
|
|
|
|
|
|
time, |
|
|
|
|
|
|
|
currentTime: 0, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
this.BackgroundAudioManager.onTimeUpdate(timeUpdateListener); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.onEnded(endedListener); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.onPlay(playListener); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.onPause(pauseListener); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.onStop(stopListener); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
listener: true, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
handlePlay() { |
|
|
|
|
|
|
|
const { chapter, currentTime, play, listener, globalStop } = this.data; |
|
|
|
|
|
|
|
if (play) return; |
|
|
|
|
|
|
|
if (this.BackgroundAudioManager) { |
|
|
|
|
|
|
|
if (this.BackgroundAudioManager.src !== chapter.AudioUrl.url || globalStop) { |
|
|
|
|
|
|
|
this.BackgroundAudioManager.title = chapter.ChapterName; |
|
|
|
|
|
|
|
this.BackgroundAudioManager.coverImgUrl = chapter.CoverUrl; |
|
|
|
|
|
|
|
this.BackgroundAudioManager.src = chapter.AudioUrl.url; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this.BackgroundAudioManager.play(); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.seek(this.BackgroundAudioManager.currentTime || currentTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!listener) { |
|
|
|
|
|
|
|
this.backgroundAudioManagerListener(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
globalStop: false, |
|
|
|
|
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
handlePause() { |
|
|
|
handlePause() { |
|
|
|
if (this.innerAudioContext) { |
|
|
|
const { play } = this.data; |
|
|
|
this.innerAudioContext.pause(); |
|
|
|
if (this.BackgroundAudioManager && play) { |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
|
|
play: false, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
this.BackgroundAudioManager.pause(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
handleDragStart() { |
|
|
|
handleDragStart() { |
|
|
@ -244,12 +293,12 @@ Page({ |
|
|
|
drag: false, |
|
|
|
drag: false, |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (this.data.play) { |
|
|
|
if (this.data.play) { |
|
|
|
this.innerAudioContext?.seek(this.data.currentTime); |
|
|
|
this.BackgroundAudioManager?.seek(this.data.currentTime); |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
handleDrag(e: any) { |
|
|
|
handleDrag(e: any) { |
|
|
|
const current = e.detail.value; |
|
|
|
const current = e.detail.value; |
|
|
|
const duration = this.innerAudioContext?.duration || this.data.chapter.AudioUrl.duration; |
|
|
|
const duration = this.BackgroundAudioManager?.duration || this.data.chapter.AudioUrl.duration; |
|
|
|
const currentTime = (current / 100) * duration; |
|
|
|
const currentTime = (current / 100) * duration; |
|
|
|
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`; |
|
|
|
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`; |
|
|
|
this.setData({ |
|
|
|
this.setData({ |
|
|
|