Advertisement
Guest User

Tiller

a guest
Mar 9th, 2008
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var FlashPlayaController = {};
  2.  
  3. //////////////////////////////////////////////////////////////////////////////
  4. // player functions
  5. //////////////////////////////////////////////////////////////////////////////
  6.  
  7. // create sound object, assign events
  8. function createSoundObject() {
  9.     soundContainer = new Sound(_level0); // define sound object
  10.     soundContainer.onSoundComplete = FlashPlayaController.songEnd; // change state at song end
  11.     soundContainer.setVolume(volumes);
  12. }
  13.  
  14. // store state and show status at end of song
  15. FlashPlayaController.songEnd = function() {
  16.     FlashPlayaController.isPlaying = false;
  17.     _level0.onEnterFrame = null; // stop tracking playstate
  18.     FlashPlayaController.strStatus = "Complete";
  19.     if (Response_mode == "playlist") {
  20.         if (FlashPlayaController.loopsong) {
  21.             FlashPlayaController.doPlay(); // replay same
  22.         } else {
  23.             FlashPlayaController.doNext(); // start next
  24.         }
  25.     } else {
  26.         if (FlashPlayaController.loopsong) {
  27.             FlashPlayaController.doPlay(); // replay same
  28.         } else {
  29.             FlashPlayaController.stopSnd();
  30.         }
  31.     }
  32. }
  33.  
  34. // load and/or play sound
  35. FlashPlayaController.playSnd = function(strUrl) {
  36.     // check playstate
  37.     if (FlashPlayaController.isPlaying) {
  38.         return; // deactivate button during play/load
  39.     } else {
  40.         FlashPlayaController.isPlaying = true;
  41.     }
  42.    
  43.     // check if same or new sound
  44.     if (FlashPlayaController.prevUrl != strUrl) { // new sound
  45.         soundContainer.loadSound(strUrl, true); // sound automatically plays after loading
  46.         FlashPlayaController.prevUrl = strUrl;
  47.     } else if (soundContainer.position > (soundContainer.duration - 100)) { // same sound
  48.         soundContainer.start(0); // replay from beginning
  49.     } else { // same sound
  50.         soundContainer.start(soundContainer.position/1000); // start sound at last paused
  51.     }
  52.     _level0.onEnterFrame = FlashPlayaController.showPlayState; // start tracking playstate
  53.     FlashPlayaController.updateTitle();
  54.     Response_btnState = "Stop";
  55. }
  56.  
  57. //
  58. FlashPlayaController.Goto = function(tmps) {
  59.     soundContainer.start(tmps/1000);
  60. }
  61.  
  62. // stop both playback and download
  63. FlashPlayaController.stopSnd = function() {
  64.     soundContainer.stop(); // stop playback
  65.     delete soundContainer; // stop download
  66.     createSoundObject(); // create a new sound object
  67.     FlashPlayaController.isPlaying = false;
  68.     FlashPlayaController.prevUrl = ""; // clear for next load
  69.     _level0.onEnterFrame = null; // stop tracking playstate
  70.     FlashPlayaController.strStatus = "Stopped";
  71.     Response_btnState = "Play";
  72. }
  73.  
  74. FlashPlayaController.pauseSnd = function() {
  75.     soundContainer.stop(); // stop playback
  76.     FlashPlayaController.isPlaying = false;
  77.     FlashPlayaController.strStatus = "Paused";
  78.     Response_btnState = "Play";
  79. }
  80.  
  81.  
  82. // show playback and load status
  83. FlashPlayaController.showPlayState = function() {
  84.     // use Number function to convert undefined values to 0
  85.     var sndPos = Number(soundContainer.position);
  86.     var loaded = (Number(soundContainer.getBytesTotal() > 0) &&
  87.         Number(soundContainer.getBytesLoaded()) == Number(soundContainer.getBytesTotal()));
  88.     if (sndPos == 0) {
  89.         FlashPlayaController.strStatus = "Loading";
  90.     } else if (sndPos <= FlashPlayaController.prevPos && !loaded) { // loading
  91.         FlashPlayaController.strStatus = "Buffering";
  92.     } else { // playing
  93.         FlashPlayaController.strStatus = "Playing";
  94.     }
  95.     FlashPlayaController.updateTitle();
  96.     FlashPlayaController.prevPos = sndPos; // store for next loop
  97. }
  98.  
  99. //////////////////////////////////////////////////////////////////////////////
  100. // xml functions
  101. //////////////////////////////////////////////////////////////////////////////
  102.  
  103. FlashPlayaController.initFromXml = function(strFile) {
  104.     AudioXml = new XML();
  105.     AudioXml.ignoreWhite = true;
  106.     AudioXml.onLoad = FlashPlayaController.LoadXmlFile;
  107.     AudioXml.load(strFile);
  108. }
  109.  
  110. FlashPlayaController.LoadXmlFile = function(success) {
  111.     if (success) {
  112.         FlashPlayaController.arPlayList = new Array();
  113.         aAudio = new Array();
  114.         aAudio = this.firstChild.childNodes;
  115.         for (i=0; i < aAudio.length; i++) {
  116.             if (aAudio[i].nodeName == "AudioProps") {
  117.                 path = aAudio[i].attributes.path;
  118.                 songTitle = (aAudio[i].attributes.songTitle) ? aAudio[i].attributes.songTitle : aAudio[i].attributes.path;
  119.                 FlashPlayaController.arPlayList.push([path, songTitle]);
  120.             }
  121.         }
  122.         Response_playlistSize = FlashPlayaController.arPlayList.length;
  123.         FlashPlayaController.strStatus = "Playlist Loaded";
  124.         FlashPlayaController.updateTitle();
  125.         // auto play on loading
  126.         if (FlashPlayaController.autoplay) {
  127.             FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  128.         }
  129.     } else {
  130.         FlashPlayaController.strStatus = "Error Loading Playlist";
  131.         FlashPlayaController.updateTitle();
  132.     }
  133. }
  134.  
  135.  
  136. //////////////////////////////////////////////////////////////////////////////
  137. // gui functions
  138. //////////////////////////////////////////////////////////////////////////////
  139.  
  140. FlashPlayaController.doPlayStop = function() {
  141.     if (FlashPlayaController.isPlaying) {
  142.         FlashPlayaController.stopSnd();
  143.     } else {
  144.         if (Response_mode == "playlist") {
  145.             FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  146.         } else {
  147.             FlashPlayaController.playSnd(urlfile);
  148.         }
  149.     }
  150. }
  151.  
  152. FlashPlayaController.doPlay = function() {
  153.     if (FlashPlayaController.isPlaying) {
  154.         return;
  155.     } else {
  156.         if (Response_mode == "playlist") {
  157.             FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  158.         } else {
  159.             FlashPlayaController.playSnd(urlfile);
  160.         }
  161.     }
  162. }
  163.  
  164. FlashPlayaController.doStop = function() {
  165.     if (FlashPlayaController.isPlaying) {
  166.         FlashPlayaController.stopSnd();
  167.     }
  168. }
  169.  
  170. FlashPlayaController.doPause = function() {
  171.     if (FlashPlayaController.isPlaying) {
  172.         FlashPlayaController.pauseSnd();
  173.     }
  174. }
  175.  
  176. FlashPlayaController.doNext = function() {
  177.     if (Response_mode == "file") {
  178.         return FlashPlayaController.doPlayStop();
  179.     }
  180.     Response_playListPosition++;
  181.     FlashPlayaController.checkRange();
  182.     FlashPlayaController.stopSnd();
  183.     FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  184. }
  185.  
  186. FlashPlayaController.doPrev = function() {
  187.     if (Response_mode == "file") {
  188.         return FlashPlayaController.doPlayStop();
  189.     }
  190.     Response_playListPosition--;
  191.     FlashPlayaController.checkRange();
  192.     FlashPlayaController.stopSnd();
  193.     FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  194. }
  195.  
  196. FlashPlayaController.doGoto = function(duree) {
  197.     if (Response_mode == "file") {
  198.         return;
  199.     }
  200.     FlashPlayaController.stopSnd();
  201.     FlashPlayaController.Goto(duree);
  202. }
  203.  
  204. FlashPlayaController.doJump = function(index) {
  205.     if (Response_mode == "file") {
  206.         return;
  207.     }
  208.     Response_playListPosition = index;
  209.     FlashPlayaController.checkRange();
  210.     FlashPlayaController.stopSnd();
  211.     FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
  212. }
  213.  
  214. FlashPlayaController.checkRange = function() {
  215.     if (Response_playListPosition < 0) {
  216.         Response_playListPosition = Response_playlistSize - 1;
  217.     }
  218.     if (Response_playListPosition >= Response_playlistSize) {
  219.         Response_playListPosition = 0;
  220.     }
  221. }
  222.  
  223. FlashPlayaController.updateTitle = function() {
  224.     if (Response_mode == "file") {
  225.         Response_title = FlashPlayaController.strStatus;
  226.         Response_path = "";
  227.         return;
  228.     }
  229.     if (FlashPlayaController.strStatus == "Playing" ||  FlashPlayaController.strStatus == "Buffering") {
  230.         Response_title = FlashPlayaController.arPlayList[Response_playListPosition][1];
  231.         Response_path = FlashPlayaController.arPlayList[Response_playListPosition][0];
  232.     } else {
  233.         Response_title = FlashPlayaController.strStatus;
  234.         Response_path = "";
  235.     }
  236. }
  237.  
  238. //////////////////////////////////////////////////////////////////////////////
  239. // remote server
  240. //////////////////////////////////////////////////////////////////////////////
  241.  
  242. FlashPlayaController.remoteServer = function() {
  243.     try {
  244.         Response_position = soundContainer.position;
  245.         Response_positionTotal = soundContainer.duration;
  246.        
  247.         if (Request_command == "PlayStop") {
  248.             FlashPlayaController.doPlayStop();
  249.         } else if (Request_command == "Prev") {
  250.             FlashPlayaController.doPrev();
  251.         } else if (Request_command == "Next") {
  252.             FlashPlayaController.doNext();
  253.         } else if (Request_command == "Position") {
  254.             Response_position = soundContainer.position;
  255.             Response_positionTotal = soundContainer.duration;
  256.         } else if (Request_command == "Play") {
  257.             FlashPlayaController.doPlay();
  258.         } else if (Request_command == "PlayUrl") {
  259.             urlfile = Request_args;
  260.             Request_args = null;
  261.             FlashPlayaController.doStop();
  262.             FlashPlayaController.doPlay();
  263.         } else if (Request_command == "Stop") {
  264.             //last_pos = soundContainer.position/1000;
  265.             FlashPlayaController.doStop();
  266.         } else if (Request_command == "Pause") {
  267.             //last_pos = soundContainer.position/1000;
  268.             FlashPlayaController.doPause();
  269.         } else if (Request_command == "PlayPosition") {
  270.             var index = Request_args;
  271.             Request_args = null;
  272.             FlashPlayaController.doJump(index);
  273.         } else if (Request_command == "Volume") {
  274.             var vol = Request_args;
  275.             Request_args = null;
  276.             volumes = vol;
  277.         } else if (Request_command == "StopTmp") {
  278.             soundContainer.stop();
  279.         } else if (Request_command == "PlayTemps") {
  280.             var duree = Request_args;
  281.             Request_args = null;
  282.             soundContainer.start(duree/1000);
  283.             if (FlashPlayaController.isPlaying == false)
  284.                 soundContainer.stop();
  285.         }
  286.         Request_command = null;
  287.         soundContainer.setVolume(volumes);
  288.     } catch (e) {
  289.         trace(e.message);
  290.     }
  291. }
  292.  
  293. //////////////////////////////////////////////////////////////////////////////
  294. // global variables
  295. //////////////////////////////////////////////////////////////////////////////
  296.  
  297. // private
  298. FlashPlayaController.prevUrl = null; // store current url in global variable
  299. FlashPlayaController.isPlaying = false; // store playstate in global variable
  300. FlashPlayaController.prevPos = 0; // store last position in global variable
  301. FlashPlayaController.strStatus = ""; // store current status in global variable
  302. FlashPlayaController.arPlayList = new Array();
  303. FlashPlayaController.loopsong = false;
  304. FlashPlayaController.autoplay = false;
  305.  
  306.  
  307. // public variables (input)
  308. var Request_command = null;
  309. var Request_args = null;
  310.  
  311. // public variables (output)
  312. var last_pos = 0;
  313. var Response_btnState = "Play";
  314. var Response_title = "";
  315. var Response_path = "";
  316. var Response_playlistSize = 0;
  317. var Response_playListPosition = 0;
  318. var Response_mode = "";
  319. var Response_position = 0;
  320. var Response_positionTotal = 0;
  321. var volumes = 100;
  322. var autoplay = false;
  323.  
  324. //////////////////////////////////////////////////////////////////////////////
  325. // init code
  326. //////////////////////////////////////////////////////////////////////////////
  327.  
  328. FlashPlayaController.run = function(urlfile, playlist) {
  329.     // import flashvars
  330.     FlashPlayaController.autoplay = false;
  331.    
  332.     if (urlfile == null) {
  333.         Response_mode = "playlist";
  334.     } else {
  335.         Response_mode = "file";
  336.     }
  337.     if (playlist == null) {
  338.         playlist = "playlist.xml";
  339.     }
  340.     if (FlashPlayaController.loopsong == "true") {
  341.         FlashPlayaController.loopsong = true;
  342.     } else {
  343.         FlashPlayaController.loopsong = false;
  344.     }
  345.  
  346.     // create initial sound object
  347.     createSoundObject();
  348.  
  349.     if (Response_mode == "playlist") {
  350.         // load the xml
  351.         FlashPlayaController.initFromXml(playlist);
  352.     } else {
  353.         // auto play on loading
  354.         if (FlashPlayaController.autoplay) {
  355.             FlashPlayaController.playSnd(urlfile);
  356.             FlashPlayaController.updateTitle();
  357.         }
  358.     }
  359.  
  360.     setInterval(FlashPlayaController.remoteServer, 100);
  361. }
  362.  
  363. FlashPlayaController.run(urlfile, playlist);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement