views:

42

answers:

2

I've googled and googled but got no where or outdated tutorials. Anyone know how to make it so I can toggle audio with buttons using ActionScript 3 on Flash?

A: 

You need to use the SoundTransform (flash.media) and SoundChannel (flash.media).

var mySound:Sound = new Sound(req);
var mySC:SoundChannel = mySound.play(1);
var myST:SoundTransform = mySC.soundTransform;
myST.volume = 0; // To mute
myST.volume = 1; // To unmute
mySC.soundTransform = myST;

This uses the soundTransform property of a SoundChannel, which lets you, among other things, control the volume. Keep in mind that you have to keep mySound and mySC, while myST will be just a variable created, for example, in a function.

Aurel300
+2  A: 

To toggle play/pause you will need to record the position of where the user has paused the audio.

To use a sound from the library like in your screenshot, you need to make that sound file available to your Actionscript.

First, Right click the sound file in your Library and click on Properties.... Inside the Properties window, check the box to Export for Actionscript. Change the Class name to something of your own, like MySong.

Now inside your code instead of pointing to an external Sound file, you will make mySound an instance of MySong.

var isPlaying:Boolean;
var pausePosition:Number;
var myChannel:SoundChannel = new SoundChannel();
// edited mySound to use an internal sound file with Class of MySong
var mySound:Sound = new MySong();
var myButton:MovieClip;

myButton.addEventListener(MouseEvent.CLICK, playPauseClicked);

myChannel = mySound.play();
isPlaying = true;

function playPauseClicked(e:MouseEvent):void
{
    if (isPlaying) {
        pausePosition = myChannel.position;
        myChannel.stop();
        isPlaying = false;
        // change the display of your button to show the pause state
    } else {
        myChannel = mySound.play(pausePosition);
        isPlaying = true;
        // change the display of your button to show the playing state
    }
}

To use an external file

You would need to use the URLRequest class to point to where the mp3 file is located. If the file was in the same directory as your published swf file it would look like this.

var mySound:Sound = new Sound(new URLRequest("whatever.mp3"));
knuckfubuck
That would go in the box I press F9 in right?\
Dan
HELP! http://img135.imageshack.us/img135/6477/hmmg.png What am I doing wrong?
Dan
"request" inside the new Sound() constructor needs to be a URLRequest pointing to the mp3 file you want to play. I should have made that clear.
knuckfubuck
Sorry I do not follow.
Dan
@Dan, see my edited answer above. I changed it to use a sound from the Library.
knuckfubuck