Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #### audio_manager.lua ####
  2.  
  3. local mgr = {}
  4.  
  5. mgr.queue = {}
  6. mgr.state = "paused"
  7.  
  8. local function next(event)
  9. local fun = table.remove(mgr.queue, 1)
  10. if fun then
  11. fun()
  12. else
  13. mgr.state = "paused"
  14. end
  15. end
  16.  
  17. local function resume ()
  18. if mgr.state == "paused" then
  19. mgr.state = "running"
  20. next()
  21. else
  22. -- assert(false, "audio playback queue already running")
  23. end
  24. end
  25.  
  26. function mgr:add (handle, delay, onStart, onEnd)
  27. delay = delay or 0
  28.  
  29. local options = {
  30. onComplete = function (event)
  31. if onEnd then onEnd() end
  32. next(event)
  33. end
  34. }
  35.  
  36. local fun = function ()
  37. audio.play(handle, options)
  38. if onStart then onStart() end
  39. end
  40.  
  41. local player = function ()
  42. timer.performWithDelay(delay, fun)
  43. end
  44.  
  45. table.insert(self.queue, player)
  46. resume()
  47. end
  48.  
  49. return mgr
  50.  
  51. #### sample usage ####
  52.  
  53.  
  54. local function onSpeakEnd()
  55. --if(event.completed == true) then
  56. --print("completed")
  57. girlmouth:pause()
  58. girlmouth:setFrame(1)
  59. --end
  60. end
  61.  
  62. local function onSpeakStart()
  63. girlmouth:play()
  64. end
  65.  
  66. local function lucySpeaks(handle, delay, callback)
  67. local onend = onSpeakEnd
  68. if callback then
  69. onend = function ()
  70. onSpeakEnd()
  71. callback()
  72. end
  73. end
  74. audio_mgr:add(handle, delay, onSpeakStart, onend)
  75. end
  76.  
  77. function issueRequest (delay)
  78. ....
  79. local request = audio.loadSound("assets/sound/specific-requests/"..foo.."-"..math.random(3)..".mp3")
  80. local callback = function ()
  81. fsm.state = "request-issued"
  82. end
  83. lucySpeaks(request, delay, callback)
  84. --audio.dispose(request)
  85. ....
  86. end
  87.  
  88. -- say greetings and issue first request
  89. local intro = audio.loadSound("assets/sound/intro/hi-i-m-lucy-"..math.random(4)..".mp3")
  90. lucySpeaks(intro)
  91. issueRequest(700)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement