Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function SoundController() {
  2.         var hasSound = false;
  3.  
  4.  
  5.         function callMP3PlayerMethod(methodName, args) {
  6.             var result = null;
  7.             try {
  8.                 if (app.mp3playerIsInit) {
  9.                     if (!args) {
  10.                         args = [];
  11.                     }
  12.  
  13.                     result = mp3player[methodName].apply(mp3player, args);
  14.                 }
  15.  
  16.             }
  17.             catch (e) {
  18.  
  19.                 try {
  20.                     GA("Radio", "SWFError", e.message + "[" + methodName, "; " + util.cloneArray(args).join(",") + "]");
  21.  
  22.                 }
  23.                 catch (e) {
  24.                     GA("Radio", "SWFError", "CallMethod:" + methodName);
  25.                 }
  26.  
  27.             }
  28.             return result;
  29.         }
  30.  
  31.  
  32.         this._play = function (url, posAsMillis) {
  33.             hasSound = true;
  34.             callMP3PlayerMethod("playMusic", [url, posAsMillis]);
  35.         };
  36.  
  37.  
  38.         this.closeMusicStream = function () {
  39.             if(hasSound){
  40.                 clearInterval(updateInterval);
  41.                 callMP3PlayerMethod("closeMusicStream");
  42.                 hasSound = false;
  43.             }
  44.  
  45.         };
  46.  
  47.         this.setVolume = function (vol) {
  48.             if(hasSound) {
  49.                 callMP3PlayerMethod("setMusicVolume", [vol / 100]);
  50.             }
  51.         };
  52.  
  53.  
  54.         var updateInterval;
  55.  
  56.         this.playSoundEffect = function (url, volume) {
  57.             callMP3PlayerMethod("setSoundEffectVolume",[volume/100]);
  58.             callMP3PlayerMethod("playSoundEffect",[url]);
  59.         };
  60.  
  61.         this.playSound = function (opts) {
  62.             clearInterval(updateInterval);
  63.             var onError = function () {
  64.                 hasSound = false;
  65.                 clearInterval(updateInterval);
  66.                 if (opts.onError) {
  67.                     opts.onError();
  68.                 }
  69.             };
  70.  
  71.             var onComplete = function () {
  72.                 hasSound = false;
  73.                 clearInterval(updateInterval);
  74.  
  75.                 if (opts.onComplete) {
  76.                     opts.onComplete();
  77.                 }
  78.             };
  79.  
  80.             window.onMP3PlayerErrorListener = onError;
  81.             window.onMP3PlayerCompleteListener = onComplete;
  82.  
  83.             var volume = util.isDef(opts.volume) ? opts.volume : 100;
  84.  
  85.             var position = opts.position || 0;
  86.  
  87.             this._play(opts.url, position);
  88.             this.setVolume(volume);
  89.  
  90.             if (opts.onUpdate) {
  91.                 updateInterval = setInterval(function () {
  92.                     var info = callMP3PlayerMethod("getMusicInfo");
  93.                     if (info) {
  94.                         opts.onUpdate(info.position);
  95.                     }
  96.                 }, 1000);
  97.                 opts.onUpdate(position);
  98.             }
  99.         }
  100.  
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement