cbeezy
cbeezy

Reputation: 97

Play a sound once in Flash

I built a Flash site that is being used as a teaching tool. The client would now like to add voiceovers to sections. Is there a way to play a sound once on a movie clip, and if the viewer comes back to that clip then the sound won't play again? Thanks.

Upvotes: 0

Views: 2385

Answers (3)

Allan
Allan

Reputation: 1083

I like zachzurn's answer. SharedObject is pretty much the only way to store cookie-like data on a machine. But I'd suggest that you also make sure to add the requisite error trapping (like try/catch blocks) because some users (myself included) have set their Flash Player settings to disallow sites from storing shared objects.

If you don't want to depend on SharedObject, then you'll probably need to setup some calls in and out of Flash to some other server-side technology. If your site requires users to log in, you may already have some persistent data for users; you could add some service calls which let Flash send and receive data about whether the sound has been played.

Upvotes: 1

zachzurn
zachzurn

Reputation: 2201

This should do the trick. I haven't tested this code in Flash but this should get you in the right direction.

import flash.net.SharedObject

//Use anything in place of "sounds"
var so:SharedObject = SharedObject.getLocal("sounds");

//Check if the sound has not been played
//Use anything in place of "specificSoundPlayed"
if(so.data.specificSoundPlayed == false)
{
//Play the sound
var snd:Sound = new Sound();
snd.load(new URLRequest("my.mp3"));
snd.play();

//Set the shared object property so we know the sound was played
so.data.specificSoundPlayed = true;
}

Upvotes: 3

citizen conn
citizen conn

Reputation: 15390

You can set a boolean flag to false once the sound has been played once.

var flag:Boolean = false;
if(!flag){ 
  flag = true;
  playSound();
}

Upvotes: 0

Related Questions