feat(station_broadcast): 新增远程音频流下载与mp3、ogg格式播放支持
新增downloadAudioBytes工具函数用于从远程URL下载音频数据并提取文件扩展名 重构stream格式音频的处理逻辑,先下载资源再根据扩展名适配播放格式 补充mp3和ogg格式的播放适配,覆盖主扬声器控制器与单个扬声器
This commit is contained in:
+43
-6
@@ -671,6 +671,26 @@ local function areHqSpeakersBusy(speakers)
|
||||
return false
|
||||
end
|
||||
|
||||
local function downloadAudioBytes(url)
|
||||
if type(url) ~= "string" or url == "" then return nil, nil, "empty url" end
|
||||
local ok, resp = pcall(function()
|
||||
return http.get(url, nil, true)
|
||||
end)
|
||||
if not ok or not resp then
|
||||
return nil, nil, "http.get failed"
|
||||
end
|
||||
|
||||
local body = resp.readAll()
|
||||
resp.close()
|
||||
if type(body) ~= "string" or #body == 0 then
|
||||
return nil, nil, "empty response body"
|
||||
end
|
||||
|
||||
local lower = url:lower()
|
||||
local ext = lower:match("%.([%a%d]+)$")
|
||||
return body, ext or "file", nil
|
||||
end
|
||||
|
||||
local function playHqAudioItem(item)
|
||||
if not Peripherals.speakers or #Peripherals.speakers == 0 then
|
||||
Utils.log("WARN", "No speakers found; cannot play audio")
|
||||
@@ -682,15 +702,30 @@ local function playHqAudioItem(item)
|
||||
if type(payload) ~= "string" or #payload == 0 then return end
|
||||
|
||||
local format = tostring(item.audioFormat or "file"):lower()
|
||||
if format == "stream" then
|
||||
local bytes, ext, err = downloadAudioBytes(payload)
|
||||
if not bytes then
|
||||
Utils.log("ERROR", "Failed to download stream audio: " .. tostring(err))
|
||||
return
|
||||
end
|
||||
payload = bytes
|
||||
if ext == "wav" then format = "wav"
|
||||
elseif ext == "mp3" then format = "mp3"
|
||||
elseif ext == "ogg" then format = "ogg"
|
||||
else format = "file" end
|
||||
end
|
||||
|
||||
local controller = Peripherals.speakers[1]
|
||||
local called = false
|
||||
|
||||
if controller then
|
||||
local ok, result = pcall(function()
|
||||
if format == "stream" and type(controller.speakStreamAll) == "function" then
|
||||
return controller.speakStreamAll(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "wav" and type(controller.speakWavAll) == "function" then
|
||||
if format == "wav" and type(controller.speakWavAll) == "function" then
|
||||
return controller.speakWavAll(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "mp3" and type(controller.speakMp3All) == "function" then
|
||||
return controller.speakMp3All(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "ogg" and type(controller.speakOggAll) == "function" then
|
||||
return controller.speakOggAll(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif type(controller.speakFileAll) == "function" then
|
||||
return controller.speakFileAll(payload, CONFIG.AUDIO_VOLUME)
|
||||
end
|
||||
@@ -703,10 +738,12 @@ local function playHqAudioItem(item)
|
||||
if not called then
|
||||
for _, sp in ipairs(Peripherals.speakers) do
|
||||
local ok, result = pcall(function()
|
||||
if format == "stream" and type(sp.speakStream) == "function" then
|
||||
return sp.speakStream(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "wav" and type(sp.speakWav) == "function" then
|
||||
if format == "wav" and type(sp.speakWav) == "function" then
|
||||
return sp.speakWav(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "mp3" and type(sp.speakMp3) == "function" then
|
||||
return sp.speakMp3(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif format == "ogg" and type(sp.speakOgg) == "function" then
|
||||
return sp.speakOgg(payload, CONFIG.AUDIO_VOLUME)
|
||||
elseif type(sp.speakFile) == "function" then
|
||||
return sp.speakFile(payload, CONFIG.AUDIO_VOLUME)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user