• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

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

0
nafty
Asked:
nafty
1 Solution
 
BillystyxCommented:
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
0
 
naftyAuthor Commented:
when the user opens the web page, I want the MP3 file to start playing.
0
 
BillystyxCommented:
have you got this in your html?
<param name="PLAY" value="false" />

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

Billystyx
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

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

Billystyx
0
 
naftyAuthor Commented:
I think I figured it out.  I added a function and made a call at the beginning of the script.
0
 
BillystyxCommented:
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
0
 
DarthModCommented:
PAQed with points (500) refunded

DarthMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now