View Full Version : play pause button question
koolkrasher
July 21st, 2008, 05:52 PM
im making a play pause button and i have hit a wall.
var snd:Sound = new Sound(new URLRequest("combination.mp3"));
btn_mc.addEventListener(MouseEvent.CLICK, playSound);
btn_mc.buttonMode =true;
function playSound(event:MouseEvent):void
{
snd.play();
btn_mc.removeEventListener(MouseEvent.CLICK, playSound);
btn_mc.addEventListener(MouseEvent.CLICK, stopSound);
}
function stopSound(event:MouseEvent):void
{
snd.stop();
btn_mc.addEventListener(MouseEvent.CLICK, playSound);
}
any ideas where im going wrong?
Largo
July 21st, 2008, 06:02 PM
If you replace snd.play(); with var sc:SoundChannel = snd.play();.
Does that work for you?
koolkrasher
July 21st, 2008, 08:46 PM
ill try that out tommorw when i get to work. that is where the file is.
koolkrasher
July 22nd, 2008, 11:21 AM
i figured it out after i read the help file in flash
package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.text.TextFieldAutoSize;
public class SoundChannel_stopExample extends Sprite {
private var snd:Sound = new Sound();
private var channel:SoundChannel = new SoundChannel();
private var button:TextField = new TextField();
public function SoundChannel_stopExample() {
var req:URLRequest = new URLRequest("MySound.mp3");
snd.load(req);
button.x = 10;
button.y = 10;
button.text = "PLAY";
button.border = true;
button.background = true;
button.selectable = false;
button.autoSize = TextFieldAutoSize.CENTER;
button.addEventListener(MouseEvent.CLICK, clickHandler);
this.addChild(button);
}
private function clickHandler(e:MouseEvent):void {
var pausePosition:int = channel.position;
if(button.text == "PLAY") {
channel = snd.play(pausePosition);
button.text = "PAUSE";
}
else {
channel.stop();
button.text = "PLAY";
}
}
}
}
Theres a nice lil play pause class comes in the help docs under SoundChannel datatype
thanks for the lead largo
koolkrasher
July 22nd, 2008, 12:50 PM
got another question. How can i start the clip from the beginning again after it is finished? I tried using an event listener to detect when the sound is complete and then reset its position but no luck.
anyone have any ideas?
Largo
July 22nd, 2008, 01:06 PM
yep you can use "the SOUND_COMPLETE" listener to make it loop, Im not sure if it this is the best way, but it should work.
I hope this helps you:
var s:Sound = new Sound(new URLRequest("MySound.mp3"));
var sw:SoundChannel = s.play();
sw.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
function soundCompleteHandler(e:Event):void {
sw = s.play();
sw.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
koolkrasher
July 25th, 2008, 12:28 PM
I did something like what you have up there but i put a stop after the play so it would loop back to the begining and stop. thanks for the help.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.