SoundPlayer.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function SoundPlayer(){
  2. base(this,LSprite,[]);
  3. this.init();
  4. }
  5. SoundPlayer.prototype.init = function() {
  6. var self = this;
  7. self.background = new LSound();
  8. background = self.background;
  9. self.background.parent = self;
  10. self.loadSound();
  11. // self.addEventListener(LEvent.REMOVED, self.onremoved);
  12. }
  13. SoundPlayer.prototype.onremoved = function() {
  14. console.log("aaaa");
  15. self.background.removeAllEventListener();
  16. }
  17. SoundPlayer.prototype.loadSound = function(){
  18. var self = this;
  19. self.backgroundLoad();
  20. };
  21. SoundPlayer.prototype.backgroundLoad = function(){
  22. var self = this;
  23. self.background.addEventListener(LEvent.COMPLETE,self.backgroundLoadComplete);
  24. self.background.load("./media/music.ogg");
  25. };
  26. SoundPlayer.prototype.backgroundLoadComplete = function(event){
  27. var self = event.currentTarget;
  28. self.parent.backgroundIsLoad = true;
  29. self.parent.setSwitcher();
  30. self.parent.setState("play");
  31. };
  32. SoundPlayer.prototype.setState = function(state) {
  33. var self = this;
  34. if(state == "play") {
  35. self.background.play();
  36. self.background.loopLength = 1000;
  37. self.switcherBitmap = new LBitmap(self.switcherOnBitmapData);
  38. } else {
  39. self.background.stop();
  40. self.switcherBitmap = new LBitmap(self.switcherOffBitmapData);
  41. }
  42. self.switcher.removeAllChild();
  43. self.switcher.addChild(self.switcherBitmap);
  44. self.state = state;
  45. }
  46. SoundPlayer.prototype.toggleState = function(event) {
  47. var self = event.target.parent.parent;
  48. if(self.state == "play") {
  49. self.setState("stop");
  50. } else {
  51. self.setState("play");
  52. }
  53. }
  54. SoundPlayer.prototype.setSwitcher = function() {
  55. var self = this;
  56. self.switcher = new LSprite();
  57. self.switcherOnBitmapData = new LBitmapData(dataList["sound_on"]);
  58. self.switcherOffBitmapData = new LBitmapData(dataList["sound_off"]);
  59. self.switcherBitmap = new LBitmap(self.switcherOnBitmapData);
  60. self.switcher.addChild(self.switcherBitmap);
  61. // self.switcher.x = LGlobal.width/2-switcherBitmap.width/2;
  62. // self.switcher.y = LGlobal.height*0.62;
  63. self.switcher.x = 10;
  64. self.switcher.y = 10;
  65. self.addChild(self.switcher);
  66. self.alpha = 0;
  67. Util.fadeIn(self, 1);
  68. self.switcher.addEventListener(LMouseEvent.MOUSE_DOWN, self.toggleState);
  69. ;~function() {
  70. var isMobile = (/mobile|Mobile/gi).test(navigator.userAgent);
  71. var blur = function() {
  72. self.lastState = self.state;
  73. self.setState("stop");
  74. };
  75. var focus = function() {
  76. self.setState(self.lastState);
  77. }
  78. if(isMobile) {
  79. window.addEventListener("pageshow", focus);
  80. window.addEventListener("pagehide", blur)
  81. } else {
  82. window.addEventListener("focus", focus);
  83. window.addEventListener("blur", blur);
  84. }
  85. }();
  86. }