尝试修复某些情况下播放音频时,处于播放状态但是进度条不走的问题

pull/2077/head
lyswhut 2024-07-26 18:50:45 +08:00
parent 98fe433819
commit 7f3ab31ad5
2 changed files with 42 additions and 5 deletions

View File

@ -12,6 +12,7 @@
- 修复 MacOS 下点击 dock 右键菜单的退出按钮时,程序没有退出的问题(#1923 - 修复 MacOS 下点击 dock 右键菜单的退出按钮时,程序没有退出的问题(#1923
- 修复 OpenAPI 的 `lyricLineAllText` 在切换到无歌词的音乐时内容没有更新的问题(#1925 - 修复 OpenAPI 的 `lyricLineAllText` 在切换到无歌词的音乐时内容没有更新的问题(#1925
- 修复切换音源时可能出现切换死循环的问题 - 修复切换音源时可能出现切换死循环的问题
- 尝试修复某些情况下播放音频时,处于播放状态但是进度条不走的问题
### 变更 ### 变更

View File

@ -51,7 +51,7 @@ let convolverDynamicsCompressor: DynamicsCompressorNode
let gainNode: GainNode let gainNode: GainNode
let panner: PannerNode let panner: PannerNode
let pitchShifterNode: AudioWorkletNode let pitchShifterNode: AudioWorkletNode
let pitchShifterNodePitchFactor: AudioParam let pitchShifterNodePitchFactor: AudioParam | null
let pitchShifterNodeLoadStatus: 'none' | 'loading' | 'unconnect' | 'connected' = 'none' let pitchShifterNodeLoadStatus: 'none' | 'loading' | 'unconnect' | 'connected' = 'none'
let pitchShifterNodeTempValue = 1 let pitchShifterNodeTempValue = 1
let defaultChannelCount = 2 let defaultChannelCount = 2
@ -119,7 +119,7 @@ const initAdvancedAudioFeatures = () => {
initConvolver() initConvolver()
initPanner() initPanner()
initGain() initGain()
// source -> analyser -> biquadFilter -> [(convolver & convolverSource)->convolverDynamicsCompressor] -> panner -> gain // source -> analyser -> biquadFilter -> pitchShifter -> [(convolver & convolverSource)->convolverDynamicsCompressor] -> panner -> gain
mediaSource = audioContext.createMediaElementSource(audio) mediaSource = audioContext.createMediaElementSource(audio)
mediaSource.connect(analyser) mediaSource.connect(analyser)
analyser.connect(biquads.get(`hz${freqs[0]}`)!) analyser.connect(biquads.get(`hz${freqs[0]}`)!)
@ -129,8 +129,43 @@ const initAdvancedAudioFeatures = () => {
convolverDynamicsCompressor.connect(panner) convolverDynamicsCompressor.connect(panner)
panner.connect(gainNode) panner.connect(gainNode)
gainNode.connect(audioContext.destination) gainNode.connect(audioContext.destination)
// 音频输出设备改变时刷新 audio node 连接
window.app_event.on('playerDeviceChanged', handleMediaListChange)
// audio.addEventListener('playing', connectAudioNode)
// audio.addEventListener('pause', disconnectAudioNode)
// audio.addEventListener('waiting', disconnectAudioNode)
// audio.addEventListener('emptied', disconnectAudioNode)
// if (!audio.paused) connectAudioNode()
} }
const handleMediaListChange = () => {
mediaSource.disconnect()
mediaSource.connect(analyser)
}
// let isConnected = true
// const connectAudioNode = () => {
// if (isConnected) return
// console.log('connect Node')
// mediaSource.connect(analyser)
// isConnected = true
// if (pitchShifterNodeTempValue == 1 && pitchShifterNodeLoadStatus == 'connected') {
// disconnectPitchShifterNode()
// }
// }
// const disconnectAudioNode = () => {
// if (!isConnected) return
// console.log('disconnect Node')
// mediaSource.disconnect()
// isConnected = false
// if (pitchShifterNodeTempValue == 1 && pitchShifterNodeLoadStatus == 'connected') {
// disconnectPitchShifterNode()
// }
// }
export const getAudioContext = () => { export const getAudioContext = () => {
initAdvancedAudioFeatures() initAdvancedAudioFeatures()
return audioContext return audioContext
@ -140,8 +175,8 @@ let unsubMediaListChangeEvent: (() => void) | null = null
export const setMaxOutputChannelCount = (enable: boolean) => { export const setMaxOutputChannelCount = (enable: boolean) => {
if (enable) { if (enable) {
initAdvancedAudioFeatures() initAdvancedAudioFeatures()
audioContext.destination.channelCount = audioContext.destination.maxChannelCount
audioContext.destination.channelCountMode = 'max' audioContext.destination.channelCountMode = 'max'
audioContext.destination.channelCount = audioContext.destination.maxChannelCount
// navigator.mediaDevices.addEventListener('devicechange', handleMediaListChange) // navigator.mediaDevices.addEventListener('devicechange', handleMediaListChange)
if (!unsubMediaListChangeEvent) { if (!unsubMediaListChangeEvent) {
let handleMediaListChange = () => { let handleMediaListChange = () => {
@ -287,7 +322,7 @@ const connectPitchShifterNode = () => {
// convolverDynamicsCompressor.connect(pitchShifterNode) // convolverDynamicsCompressor.connect(pitchShifterNode)
// pitchShifterNode.connect(panner) // pitchShifterNode.connect(panner)
pitchShifterNodeLoadStatus = 'connected' pitchShifterNodeLoadStatus = 'connected'
pitchShifterNodePitchFactor.value = pitchShifterNodeTempValue pitchShifterNodePitchFactor!.value = pitchShifterNodeTempValue
} }
const disconnectPitchShifterNode = () => { const disconnectPitchShifterNode = () => {
console.log('disconnect Pitch Shifter Node') console.log('disconnect Pitch Shifter Node')
@ -296,6 +331,7 @@ const disconnectPitchShifterNode = () => {
lastBiquadFilter.connect(convolver) lastBiquadFilter.connect(convolver)
lastBiquadFilter.connect(convolverSourceGainNode) lastBiquadFilter.connect(convolverSourceGainNode)
pitchShifterNodeLoadStatus = 'unconnect' pitchShifterNodeLoadStatus = 'unconnect'
pitchShifterNodePitchFactor = null
audio!.removeEventListener('playing', connectNode) audio!.removeEventListener('playing', connectNode)
audio!.removeEventListener('pause', disconnectNode) audio!.removeEventListener('pause', disconnectNode)
@ -337,7 +373,7 @@ export const setPitchShifter = (val: number) => {
case 'connected': case 'connected':
// a: 1 = 半音 // a: 1 = 半音
// value = 2 ** (a / 12) // value = 2 ** (a / 12)
pitchShifterNodePitchFactor.value = val pitchShifterNodePitchFactor!.value = val
break break
case 'unconnect': case 'unconnect':
connectPitchShifterNode() connectPitchShifterNode()