Advertisement
Lanzr

http_player.lua

Oct 24th, 2022
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. local dfpwm = require("cc.audio.dfpwm")
  2. local speaker = peripheral.find("speaker")
  3. --[[ chinese tips:
  4. step:
  5. 1 让设备连上一个 扬声器
  6. 2 让设备上有此程序
  7. 3 运行此程序,需要一个 dfpwm 格式文件直链
  8. 4 听歌
  9.  
  10. dfpwm : 一种音频格式编码可以参考这里去做 https://tweaked.cc/library/cc.audio.dfpwm.html 或者直接用现成的codec工具
  11. 文件直链: 可以使用网盘之类的来获得,比如 cowtransfer
  12.  
  13. codec 工具: 网页: https://music.madefor.cc/
  14. java: https://github.com/gamax92/LionRay/
  15. 跨平台: ffmpeg
  16.  
  17. 当然理想状态就是自己挂个http服务器 然后直接通过http服务来直接一步到位 mp3 解码 pcm, 然后传到设备上, 不过速度有待验证
  18. ]]--
  19. if(speaker == nil) then
  20. print("you should attach or insert a speaker in this device")
  21. elseif (arg[1] == nil) then
  22. print("you need to input the URL behind this program")
  23. end
  24.  
  25. local decoder = dfpwm.make_decoder()
  26. local data = http.get(arg[1], nil , true)
  27.  
  28. while 1 do
  29. local cksize = 16*1024
  30. local chunk = data.read(cksize)
  31.  
  32. local decoded = decoder(chunk)
  33. while not speaker.playAudio(decoded) do
  34. os.pullEvent("speaker_audio_empty")
  35. end
  36. if(string.len(chunk) < cksize ) then
  37. break
  38. end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement