Skip to main content
safiahn40752198
Known Participant
January 3, 2016
Question

ActionScript 3.0 Button play and pause for Sound and Audio

  • January 3, 2016
  • 1 reply
  • 5789 views

Sound - background music for game (use looping)

Audio - dialogue , shoot sound, explosion, etc (by frame)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

For sound, i use different scene different music,

example,

scene 1 = blues.mp3

scene 2 = magic.mp3

scene 3 = front.mp3

For audio, i use different scene different audio, and i arranged it by frame

example,

     scene 1

          frame 1-10 = dialogue1.mp3

          frame 11-20 = dialogue2.mp3

          shoot button = shoot.mp3

          jump button = jump.mp3

     scene 2

          frame 1-5=dialogue3.mp3

          frame 6-10=dialogue.mp3

          shoot button =shoot.mp3

          jump button = jump.mp3

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The problem is each scene has setting button, in setting button has option button

For option button have two button, audio button and sound button, either to mute or play.

                    Scene 1 > Setting > Option > Audio/Sound (play/mute)

My questions are,

How do I assign sound and audio?

How do I call sound and audio for each scene(sound) and frame(audio)?

How do I play or mute,

          example, right now in scene 3, if i mute sound and play audio,

               so how to make all sound in each scene are mute, but all audio are play?

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

i found these codes in other website, i think these code will help me, I tried,

But at the end, i just make myself more confused.

Look like puzzle to me.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////// CODE 1

and think this should be put in the first scene,

      Scene 1 > Setting > Option > Audio/Sound (play/mute) <---- i put this code here

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 front:Sound = new front();

var blues:Sound = new blues();

var magic:Sound = new magic();

/*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 = "front";

        break;

        case 2:

            songName = "blues";

        break;

        case 3:

            songName = "magic";

        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();

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////   CODE 2

_global.music = new Sound();

_global.music.attachSound("background_sound");

_global.music.start();

_global.music.onSoundComplete= function() {

this.start();

};

and here's what i wrote in the first frame of every scene:

var myvariable:String;

myvariable = "play";

function setVar1() {

myvariable = "stop";

}

function setVar2() {

myvariable = "play";

}

this.music_mc.onRelease=function() {

switch (myvariable) {

case "play" :

setVar1();

music_mc.gotoAndPlay("stopframe");

music.stop();

break;

case "stop" :

setVar2();

music_mc.gotoAndPlay("playframe");

music.start();

break;

}

};


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////// CODE 3


import flash.media.SoundChannel;

import flash.events.MouseEvent;

import flash.events.Event;

var ju:Boolean = true;

var lastposition:Number = 0;

var mysound:music = new music();

var soundchannel:SoundChannel = new SoundChannel();//So now that we can play the sound, how do we stop it? You can NOT stop the sound using the Sound class (e.g. mysound.stop(); will not work). Another class is used for that - the SoundChannel class - which has a stop() method that will let you stop a sound's playback.

soundchannel = mysound.play(0,0);// playing sound in the channel

soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete1);

  function onPlaybackComplete1(event:Event):void

  {

  lastposition = 0;

  soundchannel.stop();

  btn.btn_pause.visible = false;

  trace("finished");

  ju=false;

  }

  /************end of part 1********/

btn.addEventListener(MouseEvent.CLICK, playsound);

function playsound(event:MouseEvent):void

{

  if (! ju)

  {

  soundchannel = mysound.play(lastposition,0);

  btn.btn_pause.visible = true;

  ju = true;

  }

  else

  {

  lastposition = soundchannel.position;

  soundchannel.stop();

  btn.btn_pause.visible = false;

  /*trace(lastposition.toFixed(0), mysound.length.toFixed(0));*/

  ju = false;

  }

soundchannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

  function onPlaybackComplete(event:Event):void

  {

  lastposition = 0;

  soundchannel.stop();

  btn.btn_pause.visible = false;

  trace("finished");

  ju=false;

  }

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
January 3, 2016

Your best bet at understanding how to code sound is to take a step back from the game for a bit and just become familiar with using the Sound and Sound Channel classes.  Use Google and find a tutorial using search terms such as "AS3 Sound tutorial"   After you have that in hand, then think about having separate Boolean variables for the audio and background sounds that you use in conditionals to decide whether or not to allow sound to play.

Your section labeled CODE 2 appears to be AS2, so don't refer to that.

safiahn40752198
Known Participant
January 4, 2016

Thank you so much