Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component =require("component")
- local event = require("event")
- local os =require("os")
- local math= require("math")
- local fm =component.openfm_radio
- local urlprefix = "http://note.guuuu.net:5000"
- local net = component.internet
- local json = require("json")
- local timewaitingload = 10
- local EventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
- local char_w = string.byte("w")
- local char_q = string.byte("q")
- local char_s = string.byte("s")
- local running = true
- local nextmusic = 1
- local starttime = 0
- local duration = 0
- local musictab = nil
- function unknownEvent()
- -- do nothing if the event wasn't relevant
- end
- function handleEvent(eventID, ...)
- if (eventID) then
- EventHandlers[eventID](...)
- end
- end
- function EventHandlers.key_up(adress, char, code, playerName)
- if (char == char_q) then
- running = false
- end
- if (char == char_s) then
- nextmusic,starttime,duration=playmusic(musictab,nextmusic)
- print("playing . starttime = "..starttime .." duration= "..duration)
- end
- end
- function getHttpJson(url)
- local r=net.request(url)
- local j=""
- print("url = "..url)
- os.sleep(1)
- print(r.response())
- os.sleep(1)
- rd=r.read()
- while rd~=nil do
- os.sleep(0.1)
- j=j..rd
- rd=r.read()
- end
- return json.decode(j)
- end
- function playmusic(musictab,index)
- local nextindex=0
- local starttime=0
- local url= musictab[index]["url"]
- local urla=urlprefix..url
- local duration=musictab[index]["duration"]
- fm.stop()
- os.sleep(1)
- fm.setURL(urla)
- os.sleep(1)
- fm.play()
- starttime=getTimeInSecond()
- if index ~= #musictab then
- nextindex =index+1
- else
- nextindex =1
- end
- return nextindex,starttime,duration
- end
- function getTimeInSecond()
- return os.time()/100
- end
- function main()
- musictab = getHttpJson(urlprefix)
- while running do
- if getTimeInSecond() - starttime > duration + timewaitingload then
- nextmusic,starttime,duration=playmusic(musictab,nextmusic)
- print("playing . starttime = "..starttime .." duration= "..duration)
- end
- handleEvent(event.pull(1))
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement