Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var FlashPlayaController = {};
- //////////////////////////////////////////////////////////////////////////////
- // player functions
- //////////////////////////////////////////////////////////////////////////////
- // create sound object, assign events
- function createSoundObject() {
- soundContainer = new Sound(_level0); // define sound object
- soundContainer.onSoundComplete = FlashPlayaController.songEnd; // change state at song end
- soundContainer.setVolume(volumes);
- }
- // store state and show status at end of song
- FlashPlayaController.songEnd = function() {
- FlashPlayaController.isPlaying = false;
- _level0.onEnterFrame = null; // stop tracking playstate
- FlashPlayaController.strStatus = "Complete";
- if (Response_mode == "playlist") {
- if (FlashPlayaController.loopsong) {
- FlashPlayaController.doPlay(); // replay same
- } else {
- FlashPlayaController.doNext(); // start next
- }
- } else {
- if (FlashPlayaController.loopsong) {
- FlashPlayaController.doPlay(); // replay same
- } else {
- FlashPlayaController.stopSnd();
- }
- }
- }
- // load and/or play sound
- FlashPlayaController.playSnd = function(strUrl) {
- // check playstate
- if (FlashPlayaController.isPlaying) {
- return; // deactivate button during play/load
- } else {
- FlashPlayaController.isPlaying = true;
- }
- // check if same or new sound
- if (FlashPlayaController.prevUrl != strUrl) { // new sound
- soundContainer.loadSound(strUrl, true); // sound automatically plays after loading
- FlashPlayaController.prevUrl = strUrl;
- } else if (soundContainer.position > (soundContainer.duration - 100)) { // same sound
- soundContainer.start(0); // replay from beginning
- } else { // same sound
- soundContainer.start(soundContainer.position/1000); // start sound at last paused
- }
- _level0.onEnterFrame = FlashPlayaController.showPlayState; // start tracking playstate
- FlashPlayaController.updateTitle();
- Response_btnState = "Stop";
- }
- //
- FlashPlayaController.Goto = function(tmps) {
- soundContainer.start(tmps/1000);
- }
- // stop both playback and download
- FlashPlayaController.stopSnd = function() {
- soundContainer.stop(); // stop playback
- delete soundContainer; // stop download
- createSoundObject(); // create a new sound object
- FlashPlayaController.isPlaying = false;
- FlashPlayaController.prevUrl = ""; // clear for next load
- _level0.onEnterFrame = null; // stop tracking playstate
- FlashPlayaController.strStatus = "Stopped";
- Response_btnState = "Play";
- }
- FlashPlayaController.pauseSnd = function() {
- soundContainer.stop(); // stop playback
- FlashPlayaController.isPlaying = false;
- FlashPlayaController.strStatus = "Paused";
- Response_btnState = "Play";
- }
- // show playback and load status
- FlashPlayaController.showPlayState = function() {
- // use Number function to convert undefined values to 0
- var sndPos = Number(soundContainer.position);
- var loaded = (Number(soundContainer.getBytesTotal() > 0) &&
- Number(soundContainer.getBytesLoaded()) == Number(soundContainer.getBytesTotal()));
- if (sndPos == 0) {
- FlashPlayaController.strStatus = "Loading";
- } else if (sndPos <= FlashPlayaController.prevPos && !loaded) { // loading
- FlashPlayaController.strStatus = "Buffering";
- } else { // playing
- FlashPlayaController.strStatus = "Playing";
- }
- FlashPlayaController.updateTitle();
- FlashPlayaController.prevPos = sndPos; // store for next loop
- }
- //////////////////////////////////////////////////////////////////////////////
- // xml functions
- //////////////////////////////////////////////////////////////////////////////
- FlashPlayaController.initFromXml = function(strFile) {
- AudioXml = new XML();
- AudioXml.ignoreWhite = true;
- AudioXml.onLoad = FlashPlayaController.LoadXmlFile;
- AudioXml.load(strFile);
- }
- FlashPlayaController.LoadXmlFile = function(success) {
- if (success) {
- FlashPlayaController.arPlayList = new Array();
- aAudio = new Array();
- aAudio = this.firstChild.childNodes;
- for (i=0; i < aAudio.length; i++) {
- if (aAudio[i].nodeName == "AudioProps") {
- path = aAudio[i].attributes.path;
- songTitle = (aAudio[i].attributes.songTitle) ? aAudio[i].attributes.songTitle : aAudio[i].attributes.path;
- FlashPlayaController.arPlayList.push([path, songTitle]);
- }
- }
- Response_playlistSize = FlashPlayaController.arPlayList.length;
- FlashPlayaController.strStatus = "Playlist Loaded";
- FlashPlayaController.updateTitle();
- // auto play on loading
- if (FlashPlayaController.autoplay) {
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- }
- } else {
- FlashPlayaController.strStatus = "Error Loading Playlist";
- FlashPlayaController.updateTitle();
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // gui functions
- //////////////////////////////////////////////////////////////////////////////
- FlashPlayaController.doPlayStop = function() {
- if (FlashPlayaController.isPlaying) {
- FlashPlayaController.stopSnd();
- } else {
- if (Response_mode == "playlist") {
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- } else {
- FlashPlayaController.playSnd(urlfile);
- }
- }
- }
- FlashPlayaController.doPlay = function() {
- if (FlashPlayaController.isPlaying) {
- return;
- } else {
- if (Response_mode == "playlist") {
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- } else {
- FlashPlayaController.playSnd(urlfile);
- }
- }
- }
- FlashPlayaController.doStop = function() {
- if (FlashPlayaController.isPlaying) {
- FlashPlayaController.stopSnd();
- }
- }
- FlashPlayaController.doPause = function() {
- if (FlashPlayaController.isPlaying) {
- FlashPlayaController.pauseSnd();
- }
- }
- FlashPlayaController.doNext = function() {
- if (Response_mode == "file") {
- return FlashPlayaController.doPlayStop();
- }
- Response_playListPosition++;
- FlashPlayaController.checkRange();
- FlashPlayaController.stopSnd();
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- }
- FlashPlayaController.doPrev = function() {
- if (Response_mode == "file") {
- return FlashPlayaController.doPlayStop();
- }
- Response_playListPosition--;
- FlashPlayaController.checkRange();
- FlashPlayaController.stopSnd();
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- }
- FlashPlayaController.doGoto = function(duree) {
- if (Response_mode == "file") {
- return;
- }
- FlashPlayaController.stopSnd();
- FlashPlayaController.Goto(duree);
- }
- FlashPlayaController.doJump = function(index) {
- if (Response_mode == "file") {
- return;
- }
- Response_playListPosition = index;
- FlashPlayaController.checkRange();
- FlashPlayaController.stopSnd();
- FlashPlayaController.playSnd(FlashPlayaController.arPlayList[Response_playListPosition][0]);
- }
- FlashPlayaController.checkRange = function() {
- if (Response_playListPosition < 0) {
- Response_playListPosition = Response_playlistSize - 1;
- }
- if (Response_playListPosition >= Response_playlistSize) {
- Response_playListPosition = 0;
- }
- }
- FlashPlayaController.updateTitle = function() {
- if (Response_mode == "file") {
- Response_title = FlashPlayaController.strStatus;
- Response_path = "";
- return;
- }
- if (FlashPlayaController.strStatus == "Playing" || FlashPlayaController.strStatus == "Buffering") {
- Response_title = FlashPlayaController.arPlayList[Response_playListPosition][1];
- Response_path = FlashPlayaController.arPlayList[Response_playListPosition][0];
- } else {
- Response_title = FlashPlayaController.strStatus;
- Response_path = "";
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // remote server
- //////////////////////////////////////////////////////////////////////////////
- FlashPlayaController.remoteServer = function() {
- try {
- Response_position = soundContainer.position;
- Response_positionTotal = soundContainer.duration;
- if (Request_command == "PlayStop") {
- FlashPlayaController.doPlayStop();
- } else if (Request_command == "Prev") {
- FlashPlayaController.doPrev();
- } else if (Request_command == "Next") {
- FlashPlayaController.doNext();
- } else if (Request_command == "Position") {
- Response_position = soundContainer.position;
- Response_positionTotal = soundContainer.duration;
- } else if (Request_command == "Play") {
- FlashPlayaController.doPlay();
- } else if (Request_command == "PlayUrl") {
- urlfile = Request_args;
- Request_args = null;
- FlashPlayaController.doStop();
- FlashPlayaController.doPlay();
- } else if (Request_command == "Stop") {
- //last_pos = soundContainer.position/1000;
- FlashPlayaController.doStop();
- } else if (Request_command == "Pause") {
- //last_pos = soundContainer.position/1000;
- FlashPlayaController.doPause();
- } else if (Request_command == "PlayPosition") {
- var index = Request_args;
- Request_args = null;
- FlashPlayaController.doJump(index);
- } else if (Request_command == "Volume") {
- var vol = Request_args;
- Request_args = null;
- volumes = vol;
- } else if (Request_command == "StopTmp") {
- soundContainer.stop();
- } else if (Request_command == "PlayTemps") {
- var duree = Request_args;
- Request_args = null;
- soundContainer.start(duree/1000);
- if (FlashPlayaController.isPlaying == false)
- soundContainer.stop();
- }
- Request_command = null;
- soundContainer.setVolume(volumes);
- } catch (e) {
- trace(e.message);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // global variables
- //////////////////////////////////////////////////////////////////////////////
- // private
- FlashPlayaController.prevUrl = null; // store current url in global variable
- FlashPlayaController.isPlaying = false; // store playstate in global variable
- FlashPlayaController.prevPos = 0; // store last position in global variable
- FlashPlayaController.strStatus = ""; // store current status in global variable
- FlashPlayaController.arPlayList = new Array();
- FlashPlayaController.loopsong = false;
- FlashPlayaController.autoplay = false;
- // public variables (input)
- var Request_command = null;
- var Request_args = null;
- // public variables (output)
- var last_pos = 0;
- var Response_btnState = "Play";
- var Response_title = "";
- var Response_path = "";
- var Response_playlistSize = 0;
- var Response_playListPosition = 0;
- var Response_mode = "";
- var Response_position = 0;
- var Response_positionTotal = 0;
- var volumes = 100;
- var autoplay = false;
- //////////////////////////////////////////////////////////////////////////////
- // init code
- //////////////////////////////////////////////////////////////////////////////
- FlashPlayaController.run = function(urlfile, playlist) {
- // import flashvars
- FlashPlayaController.autoplay = false;
- if (urlfile == null) {
- Response_mode = "playlist";
- } else {
- Response_mode = "file";
- }
- if (playlist == null) {
- playlist = "playlist.xml";
- }
- if (FlashPlayaController.loopsong == "true") {
- FlashPlayaController.loopsong = true;
- } else {
- FlashPlayaController.loopsong = false;
- }
- // create initial sound object
- createSoundObject();
- if (Response_mode == "playlist") {
- // load the xml
- FlashPlayaController.initFromXml(playlist);
- } else {
- // auto play on loading
- if (FlashPlayaController.autoplay) {
- FlashPlayaController.playSnd(urlfile);
- FlashPlayaController.updateTitle();
- }
- }
- setInterval(FlashPlayaController.remoteServer, 100);
- }
- FlashPlayaController.run(urlfile, playlist);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement