fix(audio): 当扬声器不支持高音质时退回使用默认原版解码播放

This commit is contained in:
2026-08-15 23:11:25 +08:00
parent 8f70c1f1d3
commit 51968cc825
+23 -7
View File
@@ -1447,19 +1447,19 @@ end
local function playHqAudioItem(item) local function playHqAudioItem(item)
if not Peripherals.speakers or #Peripherals.speakers == 0 then if not Peripherals.speakers or #Peripherals.speakers == 0 then
Utils.log("WARN", "No speakers found; cannot play audio") Utils.log("WARN", "No speakers found; cannot play audio")
return return false
end end
local playSeq = State.interruptSeq local playSeq = State.interruptSeq
local payload = item.audioData local payload = item.audioData
if type(payload) ~= "string" or #payload == 0 then return end if type(payload) ~= "string" or #payload == 0 then return false end
local format = tostring(item.audioFormat or "file"):lower() local format = tostring(item.audioFormat or "file"):lower()
if format == "stream" then if format == "stream" then
local bytes, ext, err = downloadAudioBytes(payload) local bytes, ext, err = downloadAudioBytes(payload)
if not bytes then if not bytes then
Utils.log("ERROR", "Failed to download stream audio: " .. tostring(err)) Utils.log("ERROR", "Failed to download stream audio: " .. tostring(err))
return return false
end end
Utils.log("INFO", "Downloaded stream audio: " .. tostring(#bytes) .. " bytes") Utils.log("INFO", "Downloaded stream audio: " .. tostring(#bytes) .. " bytes")
payload = bytes payload = bytes
@@ -1467,6 +1467,8 @@ local function playHqAudioItem(item)
elseif ext == "mp3" then format = "mp3" elseif ext == "mp3" then format = "mp3"
elseif ext == "ogg" then format = "ogg" elseif ext == "ogg" then format = "ogg"
else format = "file" end else format = "file" end
item.audioData = payload
item.audioFormat = format
end end
local controller = Peripherals.speakers[1] local controller = Peripherals.speakers[1]
@@ -1525,19 +1527,20 @@ local function playHqAudioItem(item)
if not called then if not called then
Utils.log("ERROR", "HQ speaker did not accept audio for format " .. format) Utils.log("ERROR", "HQ speaker did not accept audio for format " .. format)
return return false
end end
while areHqSpeakersBusy(Peripherals.speakers) do while areHqSpeakersBusy(Peripherals.speakers) do
if shouldStop(playSeq) then if shouldStop(playSeq) then
Audio.stop("current") Audio.stop("current")
return return true
end end
if not waitForInterruptibleDelay(playSeq, 0.1) then if not waitForInterruptibleDelay(playSeq, 0.1) then
Audio.stop("current") Audio.stop("current")
return return true
end end
end end
return true
end end
local function pushWavChunkToSpeakers(payload, playSeq) local function pushWavChunkToSpeakers(payload, playSeq)
@@ -1781,7 +1784,20 @@ end
function Audio.playItem(item) function Audio.playItem(item)
local format = tostring(item.audioFormat or "dfpwm"):lower() local format = tostring(item.audioFormat or "dfpwm"):lower()
if format == "wav" or format == "file" or format == "stream" or format == "hls" then if format == "wav" or format == "file" or format == "stream" or format == "hls" then
return playHqAudioItem(item) local success = playHqAudioItem(item)
if success == false then
format = tostring(item.audioFormat or "file"):lower()
Utils.log("WARN", "HQ speaker playback failed, falling back to default decoding for format: " .. format)
if format == "wav" or format == "file" then
return Audio.playWavStreamItem(item)
elseif format == "dfpwm" then
return Audio.playDfpwmItem(item)
else
Utils.log("ERROR", "Cannot fallback for format: " .. format)
return false
end
end
return success
elseif format == "pcm" then elseif format == "pcm" then
return Audio.playPcmItem(item) return Audio.playPcmItem(item)
elseif format == "wav-stream" then elseif format == "wav-stream" then