Advertisement
NoelGu

GuguMusic

Jun 8th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. local component =require("component")
  2. local event = require("event")
  3. local os =require("os")
  4. local math= require("math")
  5. local fm =component.openfm_radio
  6. local urlprefix = "http://note.guuuu.net:5000"
  7. local net = component.internet
  8. local json = require("json")
  9. local timewaitingload = 10
  10. local EventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
  11. local char_w = string.byte("w")
  12. local char_q     = string.byte("q")
  13. local char_s     = string.byte("s")
  14. local running = true
  15. local nextmusic = 1
  16. local starttime = 0
  17. local duration = 0
  18. local musictab = nil
  19.  
  20. function unknownEvent()
  21.     -- do nothing if the event wasn't relevant
  22. end
  23.  
  24. function handleEvent(eventID, ...)
  25.     if (eventID) then
  26.       EventHandlers[eventID](...)
  27.     end
  28. end
  29.  
  30. function EventHandlers.key_up(adress, char, code, playerName)
  31.     if (char == char_q) then
  32.       running = false
  33.     end
  34.  
  35.     if (char == char_s) then
  36.         nextmusic,starttime,duration=playmusic(musictab,nextmusic)
  37.         print("playing . starttime = "..starttime .." duration= "..duration)
  38.     end
  39. end
  40.  
  41.  
  42. function getHttpJson(url)
  43.     local r=net.request(url)
  44.     local j=""
  45.     print("url = "..url)
  46.     os.sleep(1)
  47.     print(r.response())
  48.     os.sleep(1)
  49.    
  50.     rd=r.read()
  51.     while rd~=nil do
  52.         os.sleep(0.1)
  53.         j=j..rd
  54.         rd=r.read()
  55.     end
  56.     return json.decode(j)
  57. end
  58.  
  59. function playmusic(musictab,index)
  60.     local nextindex=0
  61.     local starttime=0
  62.     local url= musictab[index]["url"]
  63.     local urla=urlprefix..url
  64.     local duration=musictab[index]["duration"]
  65.     fm.stop()
  66.     os.sleep(1)
  67.     fm.setURL(urla)
  68.     os.sleep(1)
  69.     fm.play()
  70.    
  71.     starttime=getTimeInSecond()
  72.  
  73.     if index ~= #musictab then
  74.         nextindex =index+1
  75.     else
  76.         nextindex =1
  77.     end
  78.  
  79.     return nextindex,starttime,duration
  80. end
  81.  
  82. function getTimeInSecond()
  83.     return os.time()/100
  84. end
  85.  
  86. function main()
  87.     musictab = getHttpJson(urlprefix)
  88.  
  89.     while running do        
  90.         if getTimeInSecond() - starttime > duration + timewaitingload then
  91.             nextmusic,starttime,duration=playmusic(musictab,nextmusic)
  92.             print("playing . starttime = "..starttime .." duration= "..duration)
  93.         end
  94.         handleEvent(event.pull(1))
  95.     end
  96. end
  97.  
  98. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement