Stopping a sound in AS3 - actionscript-3

I'm making a little interactive ad for an album. It has 4 draggable song titles, that when dropped on a target, starts the song. Trying to figure out how to stop the song that's playing when a new song is started. Here's my code:
package {
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Deftones extends MovieClip{
function Deftones() {
swerve1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
swerve1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
polt1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
polt1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
rd1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
rd1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
gauze1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
gauze1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
var sound1:Sound= new Sound();
var sound2:Sound= new Sound();
var sound3:Sound= new Sound();
var sound4:Sound= new Sound();
sound1.load(new URLRequest("music/Swerve_City.mp3"));
sound2.load(new URLRequest("music/Poltergeist.mp3"));
sound3.load(new URLRequest("music/Romantic_Dreams.mp3"));
sound4.load(new URLRequest("music/Gauze.mp3"));
var channel:SoundChannel= new SoundChannel();
function dragStartS(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndS(e:MouseEvent){
e.currentTarget.stopDrag();
if (swerve1.hitTestObject(speaker1)){
channel.stop();
sound1.play(0);
}
else if (polt1.hitTestObject(speaker1)){
channel.stop();
sound2.play(0);
}
else if (rd1.hitTestObject(speaker1)){
channel.stop();
sound3.play(0);
}
else if (gauze1.hitTestObject(speaker1)){
channel.stop();
sound4.play(0);
}
}
}
}
Any help would me much appreciated.

You can use SoundMixer.stopAll()
Stops all sounds currently playing.

You can use SoundChannel class for this, look in this link (Section 4 - Stopping a Sound)

Related

Music is not Playing

Hi Iam trying to play sound i have been already inserted the media to library but it was not playing but here trace working...
Please Someone help me to find it out Thank you...
import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.MouseEvent;
stop();
var Myrequest:URLRequest = new URLRequest("hello.wav");
var mySound:Sound = new Sound();
mySound.load(Myrequest);
musicBtn.addEventListener(MouseEvent.CLICK, OnPlay);
function OnPlay(e:MouseEvent)
{
mySound.play();
trace("ok");
}

How do I stop sound using AS3)

my sounds in code called sound1 , sound2 my sounds in class called sounda , soundb I hope find answers to fix my problem`
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
var sound1:sounda = new sounda();
var sound2:soundb = new soundb();
cbox.addItem( { label: "chose"} )
cbox.addItem( { label: "first"} )
cbox.addItem( { label: "sec"} )
cbox.addEventListener(Event.CHANGE,plays);
function plays(e:Event):void
{
if (cbox.selectedItem.label == "first")
{
sound1.play();
//not working
sound2.stop();
}
if (cbox.selectedItem.label == "sec")
{
sound2.play();
//not working
sound1.stop();
}
}
//when I play sound1 then when I play sound2 .. sound1 still playing
to stop all sounds from playing
import flash.media.SoundMixer;
SoundMixer.stopAll();
to stop a single sound, you must using SoundChannel
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("myFavSong.mp3"));
myChannel = mySound.play();
// ...
myChannel.stop();

How would I play a sound when the collision occurs?

I am new to flash and using as3. I am in the process of making a simple catching game where the items fall from the top and you control a basket at the bottom to catch them. My script is fine and is playing without erros throughout which I am happy about, but how would I add a sound clip to this script to play when the item lands in the basket? Thanks in advance!!!
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
var catcher:Catcher;
var createEnemyID:uint;
var gameSpeed:uint;
var droppedText:TextField;
var caughtText:TextField;
var score:uint=0;
function initGame():void{
catcher=new Catcher();
catcher.x=500;
catcher.y=1400;
addChild(catcher);
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
Mouse.hide();
gameSpeed=500;
createEnemyID=setInterval(createEnemy,gameSpeed);
droppedText=new TextField();
droppedText.x=50;
droppedText.y=50;
addChild(droppedText);
caughtText=new TextField();
caughtText.x=250;
caughtText.y=50;
addChild(caughtText);
droppedText.text=caughtText.text='0';
}
function moveCatcher (e:MouseEvent):void{
catcher.x=this.mouseX;
e.updateAfterEvent();
}
function createEnemy():void{
var enemy:Faller=new Faller();
enemy.y=-1;
enemy.x=Math.random()*stage.stageWidth;
enemy.addEventListener (Event.ENTER_FRAME, dropEnemy);
addChild(enemy);
}
function dropEnemy(e:Event):void{
var mc:Faller=Faller(e.target);
mc.y+=15;
if(mc.hitTestObject(catcher)) {
caught(mc);
}
else if (mc.y>stage.stageHeight){
dropped(mc);
}
}
function caught(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
caughtText.text=String(Number(caughtText.text)+1);
}
function dropped(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
droppedText.text=String(Number(droppedText.text)+1);
if(droppedText.text=='5'){
gameOver();
}
}
function gameOver():void{
score=Number(caughtText.text);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
removeChild(catcher);
clearInterval(createEnemyID);
removeChild(caughtText);
removeChild(droppedText);
while(numChildren>0){
getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChildAt(0);
}
Mouse.show();
gotoAndStop('gameover');
}
initGame();
import the sound into flash.
edit the properties and set the class of the sound to MySoundClass or whatever you like but you have to reference it later.
In your code write the following in the collision method.
var sound:Sound = new MySoundClass();
sound.play();
See this AS3 Sound tutorial

How to remove stream video in next scene AS3

I am making a project with a video in a scene, but when I go to the next scene the video keeps appearing. How can I remove it.
My code so far is:
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.MouseEvent;
var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.play("short_jump.flv");
var metaListener:Object = new Object();
metaListener.onMetaData = onMetaData;
videoStream.client = metaListener;
var video:Video = new Video();
video.attachNetStream(videoStream);
stage.addChild(video);
video.x=200;
function onMetaData(data:Object):void
{
play_btn.addEventListener(MouseEvent.CLICK, playMovie);
stop_btn.addEventListener(MouseEvent.CLICK, stopMovie);
}
function playMovie(event:MouseEvent):void
{
videoStream.play("short_jump.flv");
}
function stopMovie(event:MouseEvent):void
{
videoStream.pause();
}
Thanks for your support!
video.attachCamera( null ) stops audio and video but the last frame remains in the video. you should just video.visible = false afterwards so you don't see that frame. there's a bug where clear() doesn't do what it should.

AS3 MovieClip not playing consistently

So at the beginning when my SWF loads up it also loads up a sequence of animated clips like so:
var loader:Loader = new Loader();
loader.load(new URLRequest("clips/clip4.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, clip4Loaded);
And my clipLoaded function is:
private function clip4Loaded(e:Event):void {
clip4 = e.target.content as MovieClip;
}
The clip4 file being loaded in has a stop() at the first frame. Later in the game (clip4 is the "outro") I use:
clip4.gotoAndPlay(0);
clip4.addFrameScript(clip4.totalFrames - 1, clip4End);
However, the clip only seems to play about 25% of the time and all the other clips which I load the exact same way play fine. The only difference is that those clips get played fairly soon after they load which leads me to believe clip4 is being autoreleased at some point but I really have no clue.
strange - i've got a timeline-animated swf with stop(); in the first frame and the following code:
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
/**
*
* #author www0z0k
*/
[SWF(width='400', height='300', frameRate='30')]
public class NewClass extends Sprite {
private const URL:String = 'res/1.swf';
private var spr:MovieClip;
public function NewClass():void {
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, onloaded);
ldr.load(new URLRequest(URL));
}
private function onloaded(e:Event):void {
spr = e.target.content as MovieClip;
addChild(spr);
spr.addFrameScript(spr.totalFrames - 1, function():void { x = x == 0 ? 100 : 0; } );
spr.addEventListener(MouseEvent.CLICK, onclick);
}
private function onclick(e:MouseEvent):void {
spr.gotoAndPlay(0);
}
}
}
and it works exactly as it's written.
could you please upload your clip4.swf anywhere (or test this code with it yourself)?