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.onSoundComp lete = 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.posi tion);
var loaded = (Number(soundContainer.get BytesTotal ()>0) && Number(soundContainer.getB ytesLoaded ()) == Number(soundContainer.getB ytesTotal( )));
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(u rl, true);
// sound automtically plays afer loading
prevUrl = url;
} else if (soundContainer.position>( soundConta iner.durat ion-100)) {
// same sound
soundContainer.start(0);
// replay from beginning
} else {
// same sound
soundContainer.start(sound Container. position/1 000);
// 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
//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.onSoundComp
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.posi
var loaded = (Number(soundContainer.get
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(u
// sound automtically plays afer loading
prevUrl = url;
} else if (soundContainer.position>(
// same sound
soundContainer.start(0);
// replay from beginning
} else {
// same sound
soundContainer.start(sound
// 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
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
<param name="PLAY" value="false" />
if so remove that line, or set it to true...
Billystyx
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();
playBtn.onRelease();
run your playSnd(url) function on 1st frame of the swf
Billystyx
Billystyx
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Billystyx