Revert "音频问题解决"

This reverts commit 34cad2543d.
This commit is contained in:
francis_fh
2026-01-23 12:36:27 +08:00
parent 34cad2543d
commit 7bfc765a73

View File

@@ -23,7 +23,6 @@ export function useTTSPlayer(httpUrl) {
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
const audioContext = null // 微信小程序不支持 AudioContext const audioContext = null // 微信小程序不支持 AudioContext
let innerAudioContext = null // 微信小程序音频上下文 let innerAudioContext = null // 微信小程序音频上下文
let backgroundAudioManager = null // 微信小程序背景音频管理器
// #endif // #endif
let currentAudioBuffer = null let currentAudioBuffer = null
@@ -32,64 +31,41 @@ export function useTTSPlayer(httpUrl) {
// 初始化微信小程序音频上下文 // 初始化微信小程序音频上下文
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
const initAudioManager = () => { const initInnerAudioContext = () => {
// 优先使用BackgroundAudioManager更适合真机环境 if (!innerAudioContext) {
try { innerAudioContext = uni.createInnerAudioContext()
console.log('<27> 微信小程序:创建背景音频管理器') innerAudioContext.autoplay = false
backgroundAudioManager = uni.getBackgroundAudioManager() innerAudioContext.onPlay(() => {
console.log('🎵 微信小程序音频播放开始')
// 设置默认配置
backgroundAudioManager.title = 'AI语音播报'
backgroundAudioManager.singer = 'KS AI'
backgroundAudioManager.coverImgUrl = '/static/icon/logo.png'
backgroundAudioManager.volume = 1.0
backgroundAudioManager.onPlay(() => {
console.log('🎵 微信小程序背景音频播放开始')
isSpeaking.value = true isSpeaking.value = true
isPaused.value = false isPaused.value = false
}) })
innerAudioContext.onPause(() => {
backgroundAudioManager.onPause(() => { console.log('⏸️ 微信小程序音频播放暂停')
console.log('⏸️ 微信小程序背景音频播放暂停')
isPaused.value = true isPaused.value = true
}) })
innerAudioContext.onStop(() => {
backgroundAudioManager.onStop(() => { console.log('⏹️ 微信小程序音频播放停止')
console.log('⏹️ 微信小程序背景音频播放停止')
isSpeaking.value = false isSpeaking.value = false
isComplete.value = true isComplete.value = true
}) })
innerAudioContext.onEnded(() => {
backgroundAudioManager.onEnded(() => { console.log('🎵 微信小程序音频播放结束')
console.log('🎵 微信小程序背景音频播放结束')
isSpeaking.value = false isSpeaking.value = false
isComplete.value = true isComplete.value = true
}) })
innerAudioContext.onError((res) => {
backgroundAudioManager.onError((res) => { console.error('❌ 微信小程序音频播放错误:', res.errMsg)
console.error('❌ 微信小程序背景音频播放错误:', res.errMsg, '错误码:', res.errCode)
isSpeaking.value = false isSpeaking.value = false
isComplete.value = false isComplete.value = false
}) })
innerAudioContext.onCanplay(() => {
backgroundAudioManager.onCanplay(() => { console.log('🎵 微信小程序音频可以播放了')
console.log('🎵 微信小程序背景音频可以播放了') // 只有在需要播放且未播放状态下才调用play
if (isSpeaking.value && !isPaused.value) {
innerAudioContext.play()
}
}) })
backgroundAudioManager.onWaiting(() => {
console.log('⏳ 微信小程序背景音频加载中...')
})
backgroundAudioManager.onTimeUpdate(() => {
console.log('⏱️ 微信小程序背景音频播放进度:', backgroundAudioManager.currentTime)
})
console.log('✅ 微信小程序背景音频管理器初始化成功')
return true
} catch (e) {
console.error('❌ 微信小程序背景音频管理器初始化失败:', e)
return false
} }
} }
// #endif // #endif
@@ -108,62 +84,15 @@ export function useTTSPlayer(httpUrl) {
console.log('🔗 Final GET URL:', url); console.log('🔗 Final GET URL:', url);
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
// 微信小程序环境,使用背景音频管理器 // 微信小程序环境,使用微信音频API
const isBackgroundAudioAvailable = initAudioManager() initInnerAudioContext()
console.log('🎵 微信小程序:设置音频 src 为:', url);
// 重置音频状态 // 重置音频状态
isSpeaking.value = true isSpeaking.value = true
isPaused.value = false isPaused.value = false
isComplete.value = false isComplete.value = false
// 设置src等待onCanplay事件触发后再播放
if (isBackgroundAudioAvailable && backgroundAudioManager) { innerAudioContext.src = url
console.log('🎵 微信小程序使用背景音频管理器播放URL:', url);
// 设置背景音频参数
backgroundAudioManager.title = 'AI语音播报'
backgroundAudioManager.singer = 'KS AI'
backgroundAudioManager.coverImgUrl = '/static/icon/logo.png'
// 直接设置src并播放
backgroundAudioManager.src = url
console.log('🎵 微信小程序背景音频开始播放');
} else {
// 降级方案使用InnerAudioContext
console.log('🔄 微信小程序背景音频不可用降级使用InnerAudioContext');
// 如果已有音频上下文,先销毁再重新创建
if (innerAudioContext) {
innerAudioContext.destroy()
innerAudioContext = null
}
innerAudioContext = uni.createInnerAudioContext()
innerAudioContext.autoplay = false
innerAudioContext.obeyMuteSwitch = false
innerAudioContext.volume = 1.0
innerAudioContext.onPlay(() => {
console.log('🎵 微信小程序InnerAudioContext播放开始')
isSpeaking.value = true
isPaused.value = false
})
innerAudioContext.onEnded(() => {
console.log('🎵 微信小程序InnerAudioContext播放结束')
isSpeaking.value = false
isComplete.value = true
})
innerAudioContext.onError((res) => {
console.error('❌ 微信小程序InnerAudioContext错误:', res.errMsg, '错误码:', res.errCode)
isSpeaking.value = false
isComplete.value = false
})
innerAudioContext.src = url
innerAudioContext.play()
console.log('🎵 微信小程序InnerAudioContext开始播放');
}
// #endif // #endif
// #ifdef H5 // #ifdef H5
@@ -248,29 +177,11 @@ export function useTTSPlayer(httpUrl) {
// #endif // #endif
const pause = () => { const pause = () => {
console.log('⏸️ TTS pause called');
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
// 优先使用背景音频管理器
if (backgroundAudioManager) {
try {
backgroundAudioManager.pause()
console.log('⏸️ 微信小程序背景音频暂停');
return
} catch (e) {
console.error('❌ 微信小程序背景音频暂停失败:', e);
}
}
// 降级使用InnerAudioContext
if (innerAudioContext && isSpeaking.value && !isPaused.value) { if (innerAudioContext && isSpeaking.value && !isPaused.value) {
try { console.log('⏸️ 微信小程序音频暂停');
innerAudioContext.pause() innerAudioContext.pause()
console.log('⏸️ 微信小程序InnerAudioContext暂停'); return
return
} catch (e) {
console.error('❌ 微信小程序InnerAudioContext暂停失败:', e);
}
} }
// #endif // #endif
@@ -280,40 +191,24 @@ export function useTTSPlayer(httpUrl) {
return; return;
} }
console.log('⏸️ TTS pause called');
if (audioContext.state === 'running') { if (audioContext.state === 'running') {
audioContext.suspend() audioContext.suspend()
isPaused.value = true isPaused.value = true
// 保存当前播放位置 // 保存当前播放位置
playTimeOffset = audioContext.currentTime playTimeOffset = audioContext.currentTime
console.log('✅ H5 Audio paused successfully'); console.log('✅ Audio paused successfully');
} }
// #endif // #endif
} }
const resume = () => { const resume = () => {
console.log('▶️ TTS resume called');
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
// 优先使用背景音频管理器
if (backgroundAudioManager) {
try {
backgroundAudioManager.play()
console.log('▶️ 微信小程序背景音频恢复播放');
return
} catch (e) {
console.error('❌ 微信小程序背景音频恢复失败:', e);
}
}
// 降级使用InnerAudioContext
if (innerAudioContext && isSpeaking.value && isPaused.value) { if (innerAudioContext && isSpeaking.value && isPaused.value) {
try { console.log('▶️ 微信小程序音频恢复播放');
innerAudioContext.play() innerAudioContext.play()
console.log('▶️ 微信小程序InnerAudioContext恢复播放'); return
return
} catch (e) {
console.error('❌ 微信小程序InnerAudioContext恢复失败:', e);
}
} }
// #endif // #endif
@@ -323,10 +218,12 @@ export function useTTSPlayer(httpUrl) {
return; return;
} }
console.log('▶️ TTS resume called');
if (audioContext.state === 'suspended') { if (audioContext.state === 'suspended') {
audioContext.resume() audioContext.resume()
isPaused.value = false isPaused.value = false
console.log('✅ H5 Audio resumed successfully'); console.log('✅ Audio resumed successfully');
} }
// #endif // #endif
} }
@@ -339,25 +236,12 @@ export function useTTSPlayer(httpUrl) {
console.log('⏹️ TTS stop called'); console.log('⏹️ TTS stop called');
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
// 优先使用背景音频管理器
if (backgroundAudioManager) {
try {
backgroundAudioManager.stop()
console.log('✅ 微信小程序背景音频停止');
} catch (e) {
console.error('❌ 微信小程序背景音频停止失败:', e);
}
}
// 降级使用InnerAudioContext
if (innerAudioContext) { if (innerAudioContext) {
try { try {
innerAudioContext.stop() innerAudioContext.stop()
console.log('✅ 微信小程序InnerAudioContext停止'); console.log('✅ 微信小程序音频停止');
innerAudioContext.destroy()
innerAudioContext = null
} catch (e) { } catch (e) {
console.error('❌ 微信小程序InnerAudioContext停止错误:', e); console.error('❌ 微信小程序音频停止错误:', e);
} }
} }
// #endif // #endif
@@ -368,7 +252,7 @@ export function useTTSPlayer(httpUrl) {
currentSource.stop() currentSource.stop()
currentSource.disconnect() currentSource.disconnect()
} catch (e) { } catch (e) {
console.error('❌ Error stopping H5 audio source:', e); console.error('❌ Error stopping audio source:', e);
} }
currentSource = null currentSource = null
} }
@@ -377,7 +261,7 @@ export function useTTSPlayer(httpUrl) {
try { try {
audioContext.suspend() audioContext.suspend()
} catch (e) { } catch (e) {
console.error('❌ Error suspending H5 audio context:', e); console.error('❌ Error suspending audio context:', e);
} }
} }
// #endif // #endif