Link to home
Start Free TrialLog in
Avatar of nafty
nafty

asked on

Need to add autoplay to this flash application

How can I make this flash application play the sound once the page loads?  It plays fine when the button is pressed, however I also need it to play automatically.

//store current url in global variable
prevUrl = null;

// store playstate in global variable
isPlaying = true;

// store last position in global variable
prevPos = 0;

//Load the CFM Variable
var myMP3;
myMP3 = _root.mp3;

// create sound object, assign properties and events
function createSoundObject() {
      // define sound object
      soundContainer = new Sound(this);
      // change state at song end
      soundContainer.onSoundComplete = updateStatus;
      
      return;
}

// create initial sound object
createSoundObject();

// store state and show status at end of song
function updateStatus() {
      isPlaying = true;
      // stop tracking playstate
      onEnterFrame = null;
      status.text = "Complete";
      
      playingBtn._visible = false;
      stopingBtn._visible = true;
      pauseingBtn._visible = true;
}

// show playback and load status
function showPlayState() {
      // 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<=prevPos && !loaded) {
            // loading
            status.text = "Loading";
      } else {
            // playing
            status.text = "Playing";
            
      }
      prevPos = sndPos;
      // store for next loop
}

// load and/or play sound
function playSnd(url) {
      // check playstate
      if (isPlaying) {
            return;
            // deactivate button during play/load
      } else {
            isPlaying = true;
      }
      // check if same or new sound
      if (prevUrl != url) {
            // new sound
            soundContainer.loadSound(url, true);
            // sound automtically plays afer loading
            prevUrl = url;
      } 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
      }
      onEnterFrame = showPlayState;
      // start tracking playstate
}
// stop both playback and download
function stopSnd() {
      soundContainer.stop();
      // stop playback
      delete soundContainer;
      // stop download
      createSoundObject();
      // create a new sound object
      isPlaying = false;
      prevUrl = "";
      // clear for next load
      onEnterFrame = null;
      // stop tracking playstate
      status.text = "Stopped";
      
      playingBtn._visible = false;
      stopingBtn._visible = true;
      pauseingBtn._visible = true;
      
}
// pause sound
function pauseSnd() {
      soundContainer.stop();
      isPlaying = false;
      onEnterFrame = null;
      // stop tracking playstate
      status.text = "Paused";
      
      playingBtn._visible = false;
      stopingBtn._visible = false;
      pauseingBtn._visible = true;
      
}
// stop sound reset position to begining
function rewindSnd() {
      soundContainer.stop();
      // stop current playback
      soundContainer.start(0);
      // reset position to 0
      soundContainer.stop();
      // prevent imediate playback
      isPlaying = false;
      onEnterFrame = null;
      // stop tracking playstate
      status.text = "Rewound";
      
      //playBtn._visible = true;
      //playingBtn._visible = false;
      
}

this.onEnterFrame=function()
      {
            playingBtn._visible = isPlaying;
            stopingBtn._visible = not isPlaying;
            pauseingBtn._visible = not isPlaying;
      
      };

// assign button events
playBtn.onRelease = function() {
      sndurl.text=myMP3;
      playSnd(sndurl.text);
      playingBtn._visible = true;
      stopingBtn._visible = false;
      pauseingBtn._visible = false;
      
};

pauseBtn.onRelease = pauseSnd;
rewindBtn.onRelease = rewindSnd;
stopBtn.onRelease = stopSnd;

stop();
// prevent timeline looping

Avatar of Billystyx
Billystyx

When you say "play the sound once the page loads" does this mean initially - when you open flash player or the web page, or do you meanthe page inside the app?

Billystyx
Avatar of nafty

ASKER

when the user opens the web page, I want the MP3 file to start playing.
have you got this in your html?
<param name="PLAY" value="false" />

if so remove that line, or set it to true...

Billystyx
Avatar of nafty

ASKER

yes I have that param set to true, and it still doesn't work.
Add this line at the very end of that script:

playBtn.onRelease();
run your playSnd(url) function on 1st frame of the swf

Billystyx
Avatar of nafty

ASKER

I think I figured it out.  I added a function and made a call at the beginning of the script.
as in this:
soundContainer.start(0);
inside a function?
That's kind of where (I think) negatyve and me were going with it... but a separate function played once would do just as well.

Good luck with it:)

Billystyx
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial