?
Solved

Need to add autoplay to this flash application

Posted on 2005-04-26
10
Medium Priority
?
163 Views
Last Modified: 2010-05-18
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
Comment
Question by:nafty
9 Comments
 
LVL 18

Expert Comment

by:Billystyx
ID: 13873589
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
 

Author Comment

by:nafty
ID: 13873724
when the user opens the web page, I want the MP3 file to start playing.
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13873759
have you got this in your html?
<param name="PLAY" value="false" />

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

Billystyx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:nafty
ID: 13873770
yes I have that param set to true, and it still doesn't work.
0
 
LVL 27

Expert Comment

by:negatyve
ID: 13873894
Add this line at the very end of that script:

playBtn.onRelease();
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13873926
run your playSnd(url) function on 1st frame of the swf

Billystyx
0
 

Author Comment

by:nafty
ID: 13910022
I think I figured it out.  I added a function and made a call at the beginning of the script.
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13910168
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
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 15659527
PAQed with points (500) refunded

DarthMod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question