gamequery-soundwrapper-soundmanager.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * gameQuery rev. $Revision$
  3. *
  4. * Copyright (c) 2008 Selim Arsever (gamequery.onaluf.org)
  5. * licensed under the MIT (MIT-LICENSE.txt)
  6. */
  7. /**
  8. * To use this wrapper you will need:
  9. * 1) Include SoundManager2.js before this script
  10. * 2) Give SoundManager2 the position of the swf ie. : soundManager.url = './path/to/swf'
  11. * 3) Optionally deactivate the debug mode from SoundManager2
  12. */
  13. // this allows use of the convenient $ notation in a plugin
  14. (function($) {
  15. soundManager.url = './'
  16. // Here is a bogus soundWrapper written as an example
  17. $.extend($.gameQuery, {
  18. SoundWrapper: function(url, loop) {
  19. // start loading the sound. Should turn this.ready to true once done.
  20. this.load = function(){
  21. try{
  22. this.sound = soundManager.createSound({
  23. id: this.id,
  24. url: url,
  25. autoplay: false,
  26. autoLoad: true
  27. });
  28. } catch (err) {
  29. // if something failed we generate a fake sound object
  30. this.sound = {readyState: 3, play: function(){}};
  31. }
  32. };
  33. // plays the sound if this.ready == true
  34. this.play = function(){
  35. if(loop){
  36. this.sound.play({
  37. onfinish: function() {
  38. this.play();
  39. }
  40. });
  41. } else {
  42. this.sound.play();
  43. }
  44. };
  45. // pauses the sound if it is playing
  46. this.pause = function(){
  47. this.sound.pause();
  48. };
  49. // stops the sound if it is playing, rewind (even if paused)
  50. this.stop = function(){
  51. this.sound.stop();
  52. };
  53. // mutes the sound without stopping it
  54. this.muted = function(mute){
  55. if(mute){
  56. this.sound.mute()
  57. } else {
  58. this.sound.unmute();
  59. }
  60. }
  61. // returns true if the sound is ready to be played
  62. this.ready = function(){
  63. return this.sound.readyState==3
  64. };
  65. // add the sound to the manager
  66. this.id = 'sound_'+$.gameQuery.resourceManager.sounds.length;
  67. $.gameQuery.resourceManager.addSound(this);
  68. return true;
  69. }});
  70. })(jQuery);