Joseph Portuondo
Joseph Portuondo

Reputation: 37

AS3 Code to pause and play "overall" audio in flash (from scene to scene)?

My question is, how to pause the audio in "scene 1" and have it so that when you go to "scene 3" (which has a different audio file attached that is going to be played) it still has the audio in general muted but when you click on the "on" button it plays the respective song of the scene, kind of like how in games you can mute the music at the main menu but then unmute it throughout gameplay;

Also in general how would I go about making a play/pause button (I am assuming an "if else" statement might work but not sure)

Upvotes: 1

Views: 2143

Answers (2)

CodeMouse92
CodeMouse92

Reputation: 6898

This really depends on whether you want to actually PAUSE the audio, STOP the audio, or just MUTE the audio. Each has a different use.

However, for your usage, it sounds like you'll want to use a "mute" button. There are, I'm sure, a number of ways to do this. What I recommend is creating an AS3 class specifically for audio. Then, you'll want to set up this class inside of your document class.

Go to File --> New... and select ActionScript 3.0 Class. Name it "gamesounds.as". Before you click save, create a new folder in the same directory as your .fla. Name this folder "gamecore", and then save "gamesounds.as" inside of it. The reason I just did that is, you'll want to keep all your custom classes of this sort together.

Now, here's the basic structure for your class:

package gamecore
{
    public class GameSounds
    {
        //constructor code
    }
}

Before we do anything else, we need to ensure that our game will be able to access this class. We don't want to be creating a million copies, because that will undermine the class' primary functionality. Open up your document class (Making a new document class is outside the scope of this answer. Look it up.) Of course, the document class must be in the same directory as the gamecore folder (NOT IN the gamecore folder).

Above the class declaration in the document class, enter this line of code:

import gamecore.GameSounds;

Then, inside your class declaration, enter this line:

public static var GameSounds:GameSounds = new GameSounds();

Save, obviously, and then go back to your gamesounds.as file. Here's the code you'll want to use. I've added comments to illustrate what the different code does.

I'm assuming you've imported all your songs into your .fla's library, and created their actionscript bindings. You can modify this for playing songs externally, too, though I won't go into that here.

The following should replace //constructor code

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;

//Create a variable to indicate whether the music is muted.
var isMuted:Boolean = false;

/*Create a string variable for the name of the song that should be playing. Alternatively, you could just name this an int and store the scene number. It all depends on what you want to do.*/
var songName:String;

//Create a sound channel for playing audio.
var musicChannel:SoundChannel = new SoundChannel();

//Import your songs.
var fooForYou:Sound = new fooForYou();
var iveBeenAFoo:Sound = new iveBeenAFoo();
var pityTheFoo:Sound = new pityTheFoo();

/*This function sets the target song based on location. Just pass it the integer of the stage. Alternatively, you can make this work with the stage name as a String.*/
function startMusic(targetScene:int):void
{
    /*Depending on the targetScene number, set the correct song name. Note these match the song declarations above.*/
    switch(targetScene)
    {
        case 1:
            songName = "fooForYou";
        break;
        case 2:
            songName = "iveBeenAFoo";
        break;
        case 3:
            songName = "pityTheFoo;
        break;
    }
    //Start the actual music playing.
    playMusic();
}

/*This function starts the music itself. Keep it separate, in case you need to bypass the startMusic code for some reason.*/
function playMusic():void
{
    //I'd imagine you want your music looped, so int.MAX_VALUE accommodates for that.
    musicChannel = this[songName].play(0, int.MAX_VALUE);

    //Mute or unmute depending on that variable above.
    adjustVolume();
}

//This function mutes or unmutes depending on the variable condition.
function adjustVolume():void
{
    //We create a SoundTransform.
    var transform:SoundTransform = musicChannel.soundTransform;

    //We set the volume to 0 or 1, depending on the isMuted variable.
    if(isMuted)
    {
        transform.volume = 0;
    }
    else
    {
        transform.volume = 1;
    }

    //We apply the transform to the song.
    musicChannel.soundTransform = transform;
}

/*This function is present for convenience's sake. Calling this adjusts the variable AND the music that is currently playing.*/
function setMute(mute:Boolean):void
{
    //Sets the isMuted variable to the mute argument.
    isMuted = mute;

    //Mutes or unmutes the currently playing sound.
    adjustVolume();
}

Now, you only need to use two functions in your .fla itself. I'm going to assume that "DocClass" is the name of your document class. At the start of every stage, call this line of code, replacing the "1" in the argument with the stage number (or name, if you've opted for that route.)

DocClass.GameSounds.startMusic(1);

It will start the music in effect, but it can only be heard if the music isn't set to be muted.

Add this code to your mute button to mute, replacing "true" with "false" to unmute.

DocClass.GameSounds.setMute(true);

-- In regards to the pause button, this question should be asked separately. I will tell you that if you intend to loop your music, pausing it when using SoundChannel is going to create an issue for ya, in that the music will loop from the point it was last paused.

I hope that's helpful to you! I've been using that sort of code for over a year now in my project, and I've never had a problem with it.

Upvotes: 1

Tutash
Tutash

Reputation: 31

First include the SoundMixer class:

import flash.media.SoundMixer;

Then to stop the sounds call the stopAll method:

SoundMixer.stopAll();

Upvotes: 0

Related Questions