musictest.monkey2 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #rem
  2. Simple music demo, with timed lyrics
  3. Note: PlayMusic path must be a file system path so ths wont work on android yet.
  4. #end
  5. Namespace myapp
  6. #Import "<std>"
  7. #Import "<mojo>"
  8. #Import "ACDC_-_Back_In_Black-sample.ogg"
  9. Using std..
  10. Using mojo..
  11. Class MyWindow Extends Window
  12. Field _channel:Channel
  13. Field lyrics:TimedLyrics
  14. Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )
  15. Super.New( title,width,height,flags )
  16. lyrics = New TimedLyrics
  17. StartMusic()
  18. End
  19. Method StartMusic()
  20. _channel=Audio.PlayMusic( "asset::ACDC_-_Back_In_Black-sample.ogg",Lambda()
  21. Print "Music Finished!"
  22. End )
  23. End
  24. Method OnRender( canvas:Canvas ) Override
  25. RequestRender()
  26. canvas.DrawText(
  27. "Music Test: Hit [Enter] to "+
  28. (_channel ? "stop" Else "start")+
  29. (_channel ? ", [Space] to "+(_channel.Paused ? "resume" Else "pause") Else "")+
  30. (_channel ? ", Sample="+_channel.PlayheadSample+", Time="+_channel.PlayheadTime Else ""),
  31. 0,0 )
  32. If _channel lyrics.Render(canvas,_channel.PlayheadTime,Width/2,Height/2)
  33. 'Stop/Start?
  34. If Keyboard.KeyHit( Key.Enter ) Or Mouse.ButtonHit( MouseButton.Left )
  35. If _channel
  36. _channel.Stop()
  37. _channel=Null
  38. Else
  39. StartMusic()
  40. Endif
  41. Endif
  42. 'Pause/Resume?
  43. If Keyboard.KeyHit( Key.Space ) Or Mouse.ButtonHit( MouseButton.Right )
  44. If _channel
  45. _channel.Paused=Not _channel.Paused
  46. Endif
  47. Endif
  48. End
  49. End
  50. Function Main()
  51. New AppInstance
  52. New MyWindow
  53. App.Run()
  54. End
  55. Class TimedText
  56. Field time:Float
  57. Field text:String
  58. Method New(time:Float,text:String)
  59. Self.time = time
  60. Self.text = text
  61. End
  62. End
  63. Class TimedLyrics
  64. Field _lyr:= New Stack<TimedText>
  65. Method New ()
  66. _lyr.Add(New TimedText(2,"Back in black"))
  67. _lyr.Add(New TimedText(3.3,"I hit the sack"))
  68. _lyr.Add(New TimedText(4.7,"I've been too long I'm glad to be back"))
  69. _lyr.Add(New TimedText(7,"Yes, I'm let loose"))
  70. _lyr.Add(New TimedText(8.5,"From the noose"))
  71. _lyr.Add(New TimedText(10,"That's kept me hanging about"))
  72. _lyr.Add(New TimedText(12.5,"I've been looking at the sky"))
  73. _lyr.Add(New TimedText(13.2,"'Cause it's gettin' me high"))
  74. _lyr.Add(New TimedText(15,"Forget the hearse 'cause I never die"))
  75. _lyr.Add(New TimedText(17,"I got nine lives"))
  76. _lyr.Add(New TimedText(19.3,"Cat's eyes"))
  77. _lyr.Add(New TimedText(20.5,"Abusin' every one of them and running wild"))
  78. End
  79. Method FindText:String (time:Float)
  80. Local bestmatch:String
  81. For Local tt:= Eachin _lyr
  82. If time>=tt.time
  83. bestmatch = tt.text
  84. Endif
  85. Next
  86. Return bestmatch
  87. End
  88. Method Render(canvas:Canvas,time:Float,x:Int,y:Int)
  89. canvas.DrawText(FindText(time),x,y,0.5,0.5)
  90. End
  91. End