SoundController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. SoundController.FX = "fx";
  2. SoundController.MUSIC = "music";
  3. function getHiddenProp(){
  4. var prefixes = ['webkit','moz','ms','o'];
  5. // if 'hidden' is natively supported just return it
  6. if ('hidden' in document) return 'hidden';
  7. // otherwise loop over all the known prefixes until we find one
  8. for (var i = 0; i < prefixes.length; i++){
  9. if ((prefixes[i] + 'Hidden') in document)
  10. return prefixes[i] + 'Hidden';
  11. }
  12. // otherwise it's not supported
  13. return null;
  14. }
  15. function isHidden() {
  16. var prop = getHiddenProp();
  17. if (!prop) return false;
  18. return document[prop];
  19. }
  20. function visChange() {
  21. SoundController.getInstance().muteSound(isHidden());
  22. }
  23. //************* SINGLETON ****************//
  24. SoundController.getInstance = function(){
  25. if(SoundController.prototype.instance == null){
  26. SoundController.prototype.instance = new SoundController();
  27. }
  28. return SoundController.prototype.instance;
  29. }
  30. SoundController.pauseBGM = function() {
  31. SoundController.getInstance().muteSound(true);
  32. }
  33. SoundController.resumeBGM = function() {
  34. SoundController.getInstance().muteSound(false);
  35. }
  36. function onVisibilityChange(evt) {
  37. if(evt.target.webkitHidden) {
  38. SoundController.getInstance().muteSound(true);
  39. } else {
  40. SoundController.getInstance().muteSound(false);
  41. }
  42. }
  43. function SoundController(){
  44. this.sStatus = {fx:false, music:false};
  45. this.isMuted = false;
  46. var visProp = getHiddenProp();
  47. if (visProp) {
  48. var evtname = visProp.replace(/[H|h]idden/,'') + 'visibilitychange';
  49. window.addEventListener("blur", SoundController.pauseBGM, false);
  50. window.addEventListener("pagehide", SoundController.pauseBGM, false);
  51. window.addEventListener("focus", SoundController.resumeBGM, false);
  52. window.addEventListener("pageshow", SoundController.resumeBGM, false);
  53. document.addEventListener(evtname, visChange);
  54. }
  55. this.soundStatus = {fx:true, music:true};
  56. var types = {};
  57. types[SoundController.FX] = {muted:false, sounds:new Array()};
  58. types[SoundController.MUSIC] = {muted:false, sounds:new Array()};
  59. this.play = function($id, $props, delay, startPosition, loop){
  60. if(arguments.length < 3) {
  61. delay = 0;
  62. }
  63. if(arguments.length < 4) {
  64. startPosition = 0;
  65. }
  66. if(arguments.length < 5) {
  67. loop = 0;
  68. }
  69. var stype = queue.getItem($id).stype;
  70. var type = types[stype];
  71. var snd = createjs.Sound.play($id, $props,delay,startPosition, loop);
  72. snd.id = $id;
  73. snd.stype = stype;
  74. snd.setMute(type.muted);
  75. type.sounds.push(snd);
  76. snd.addEventListener("complete", onComplete);
  77. return snd;
  78. }
  79. this.muteSound = function(b) {
  80. if(b) {
  81. if(this.isMuted) {
  82. return;
  83. }
  84. this.sStatus = {fx:types.fx.muted, music:types.music.muted};
  85. this.setMute('fx', true);
  86. this.setMute('music', true);
  87. this.isMuted = true;
  88. } else {
  89. if(this.isMuted === false) {
  90. return;
  91. }
  92. this.isMuted = false;
  93. this.setMute('fx', this.sStatus.fx);
  94. this.setMute('music', this.sStatus.music);
  95. }
  96. };
  97. this.findSoundAndRemove = function($id) {
  98. var o = this.findSound($id);
  99. if(o) {
  100. this.stop(o);
  101. }
  102. };
  103. this.findSound = function($id) {
  104. var stype = queue.getItem($id).stype;
  105. var type = types[stype];
  106. var size = type.sounds.length;
  107. for(var i = 0; i<size; i++){
  108. if(type.sounds[i].id == $id){
  109. return type.sounds[i];
  110. }
  111. }
  112. return null;
  113. };
  114. this.stop = function($snd){
  115. $snd.stop();
  116. removeSound($snd);
  117. }
  118. this.setMute = function($type, $muted){
  119. var type = types[$type];
  120. type.muted = $muted;
  121. this.soundStatus[$type] = !$muted;
  122. var type = types[$type];
  123. var size = type.sounds.length;
  124. var snd;
  125. type.muted = $muted;
  126. this.soundStatus[$type] = !$muted;
  127. for(var i = 0; i<size; i++){
  128. type.sounds[i].setMute($muted);
  129. }
  130. }
  131. function removeSound($snd){
  132. var type = types[$snd.stype];
  133. var size = type.sounds.length;
  134. for(var i = 0; i<size; i++){
  135. if(type.sounds[i].uniqueId == $snd.uniqueId){
  136. type.sounds.splice(i, 1);
  137. return;
  138. }
  139. }
  140. }
  141. function onComplete($event){
  142. var snd = $event.target;
  143. snd.removeEventListener("complete", onComplete);
  144. removeSound(snd);
  145. }
  146. }