home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Aventura / VenusMission.swf / scripts / __Packages / SoundManager.as < prev    next >
Encoding:
Text File  |  2008-09-23  |  4.0 KB  |  186 lines

  1. class SoundManager
  2. {
  3.    var MC;
  4.    var played;
  5.    var _vol;
  6.    var start;
  7.    static var myInstance;
  8.    var _enabled = true;
  9.    var muted_vol = 100;
  10.    function SoundManager(_MC)
  11.    {
  12.       this.MC = _MC;
  13.       this.played = new Array();
  14.       this._enabled = true;
  15.       this._vol = 100;
  16.       this.muted_vol = 100;
  17.    }
  18.    static function getInstance()
  19.    {
  20.       if(SoundManager.myInstance == undefined)
  21.       {
  22.          SoundManager.myInstance = new SoundManager();
  23.       }
  24.       return SoundManager.myInstance;
  25.    }
  26.    function setVolume(v)
  27.    {
  28.       this._vol = v;
  29.       play();
  30.    }
  31.    function enable()
  32.    {
  33.       var snd;
  34.       this._enabled = true;
  35.       this._vol = this.muted_vol;
  36.       for(var name in this.played)
  37.       {
  38.          snd = this.played[name];
  39.          snd.setVolume(this._vol);
  40.       }
  41.    }
  42.    function get enabled()
  43.    {
  44.       return this._enabled;
  45.    }
  46.    function isPlaying(soundname, MC)
  47.    {
  48.       var snd = this.played[MC != undefined ? String(MC) : soundname];
  49.       if(snd != undefined)
  50.       {
  51.          return snd.isPlaying;
  52.       }
  53.       return false;
  54.    }
  55.    function mute()
  56.    {
  57.       var snd;
  58.       this._enabled = false;
  59.       this.muted_vol = this._vol;
  60.       this._vol = 0;
  61.       for(var name in this.played)
  62.       {
  63.          snd = this.played[name];
  64.          snd.setVolume(0);
  65.       }
  66.    }
  67.    function remove(soundname)
  68.    {
  69.       if(this.played[soundname] != undefined)
  70.       {
  71.          this.stop(soundname);
  72.          delete this.played[soundname];
  73.       }
  74.    }
  75.    function pause(soundname)
  76.    {
  77.       var snd;
  78.       var p;
  79.       var d;
  80.       snd = this.played[soundname];
  81.       if(snd != undefined)
  82.       {
  83.          p = Math.round(snd.position / 1000);
  84.          d = Math.round(snd.duration / 1000);
  85.          snd.pause = p >= d ? 0 : p;
  86.          snd.isPlaying = false;
  87.          snd.stop();
  88.       }
  89.       else
  90.       {
  91.          for(var name in this.played)
  92.          {
  93.             snd = this.played[name];
  94.             p = Math.round(snd.position / 1000);
  95.             d = Math.round(snd.duration / 1000);
  96.             snd.pause = p >= d ? 0 : p;
  97.             snd.isPlaying = false;
  98.             snd.stop(name);
  99.          }
  100.       }
  101.    }
  102.    function stop(soundname, MC)
  103.    {
  104.       var snd;
  105.       if(soundname != undefined)
  106.       {
  107.          if(MC == undefined)
  108.          {
  109.             snd = this.played[soundname];
  110.          }
  111.          else
  112.          {
  113.             snd = this.played[String(MC)];
  114.          }
  115.          snd.pause = 0;
  116.          snd.isPlaying = false;
  117.          snd.stop(soundname);
  118.          delete snd;
  119.       }
  120.       else
  121.       {
  122.          for(var name in this.played)
  123.          {
  124.             snd = this.played[name];
  125.             snd.stop(name);
  126.             snd.pause = 0;
  127.             snd.isPlaying = false;
  128.             delete snd;
  129.          }
  130.       }
  131.    }
  132.    function play(soundname, loop, MC, oncomplete)
  133.    {
  134.       loop = loop == true;
  135.       var snd;
  136.       if(MC == undefined)
  137.       {
  138.          snd = this.played[soundname] = new Sound();
  139.       }
  140.       else
  141.       {
  142.          snd = this.played[String(MC)] = new Sound();
  143.       }
  144.       snd.isPlaying = true;
  145.       snd.attachSound(soundname);
  146.       snd.start();
  147.       snd.setVolume(this._vol);
  148.       if(loop)
  149.       {
  150.          snd.onSoundComplete = function()
  151.          {
  152.             this.start();
  153.          };
  154.       }
  155.       else
  156.       {
  157.          snd.onSoundComplete = function()
  158.          {
  159.             this.isPlaying = false;
  160.          };
  161.       }
  162.    }
  163.    function onComplete(soundname)
  164.    {
  165.       var snd = this.played[soundname];
  166.       if(snd == undefined)
  167.       {
  168.          return undefined;
  169.       }
  170.       if(snd.onComplete == undefined && snd.loop == false)
  171.       {
  172.          this.remove(soundname);
  173.       }
  174.       else if(snd.loop)
  175.       {
  176.          snd.onComplete();
  177.          snd.start();
  178.       }
  179.       else
  180.       {
  181.          snd.onComplete();
  182.          snd.stop();
  183.       }
  184.    }
  185. }
  186.