To play,stop and pause sound using as3 we have to use following:
flash.media.Sound : Sound class is used to handle the loading and playing a sound.
flash.media.SoundChannel: SoundChannel object is used to handle the playback like play,pause
and stop sound.
How to load external sound file:
var requestSound:URLRequest = new URLRequest("sound file path");
var sound:Sound = new Sound(req);
Sample application to play,pause and stop sound:
Here three buttons btn,btn1,btn2 is used to play,stop and pause a sound:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
public class Main extends MovieClip {
private var _snd:Sound;
private var channel:SoundChannel ;
private var bool:Boolean = true;
private var pauseValue:int;
public function Main() {
// constructor code
_snd = new Sound(new URLRequest("your sound file"));
//channel = _snd.play();
btn.addEventListener(MouseEvent.CLICK,playSound);
btn1.addEventListener(MouseEvent.CLICK,stopSound);
btn2.addEventListener(MouseEvent.CLICK,pauseSound);
}
function playSound(e:MouseEvent)
{
if(bool)
{
channel = _snd.play(pauseValue);
channel.addEventListener(Event.SOUND_COMPLETE, repeatSound);
trace("play");
}
if(!bool)
{
bool = true;
channel = _snd.play(pauseValue);
channel.addEventListener(Event.SOUND_COMPLETE, repeatSound);
}
}
function stopSound(e:MouseEvent)
{
trace("stop");
channel.stop();
}
function pauseSound(e:MouseEvent)
{
pauseValue = channel.position;
channel.stop();
bool = false;
}
function repeatSound(e:Event)
{ trace("repeat");
channel = _snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, repeatSound);
}
}
}
0 Comment(s)