Can't restart music as3 - actionscript-3

I have a button that turns on and off the music. When I turn it off it works, but when I try to play again the music it doesn't work! help please
var sound=true;
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("Track.mp3"));
myChannel = mySound.play();
function changeSound(event:MouseEvent):void {
if(sound==true){
myChannel.stop();
sound=false;
}
if(sound==false){
myChannel.reset();
sound=true;
}
}

SoundChannel doesn't have a reset method. Call play instead of reset.
if(sound==false){
myChannel = mySound.play();
sound=true;
}
Also, you should place else between two if.
if(sound==true){
myChannel.stop();
sound=false;
}
else if(sound==false){
myChannel = mySound.play();
sound=true;
}

Related

play a external flv video

Hello please someone can help me with this ...
I want to play a external flv video ("../sync/video/video.flv"), but in case the video is missing or when there is a (StreamNotFound) error
I want to play automatically another flv video.
case "NetStream.Play.StreamNotFound":
ns.play("../sync/filler/video2.flv");
but it doesn't work ....
here is the full code :
var vid:Video;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
customClient.onMetaData = metaDataHandler;
ns.client = customClient;
ns.play("../sync/video/video.flv");
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
function netStatusF(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetStream.Play.StreamNotFound" :
ns.play("../sync/filler/video2.flv");
break;
}
}
function metaDataHandler(infoObject:Object):void
{
vid.width = infoObject.width;
vid.height = infoObject.height;
}
You have just to add a NetStatusEvent.NET_STATUS event listener to your NetStream object :
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusF);
Then you have to assure that your second video file exist, otherwise you'll have a looping problem.
Hope that can help.

Lower volume of certain audio on frame with multiple audios. AS3

Okay, so, here's the deal, i have a sound steaming from an URL since the first frame (60 total), and i added another sound file (from the library) to a certain frame. As expected, both sounds overlapp, so i decided to make a mute function... the problem is, i cant get it to apply only to the streaming audio, the function mutes all audio, and i cant use the variable mySound because the mute function needs to be on other keyframe to work...
Here's the code of the streaming audio:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;// pause button
mySound.load(new URLRequest("http://trollfacequiz.16mb.com/MusicFiles/Goat%20Songs.mp3"));
//mySound.play();
SoundMixer.stopAll();
myChannel = mySound.play();
btn_mute.btn_pause.addEventListener(MouseEvent.CLICK, onClickPause);
btn_mute.btn_play.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPause(e:MouseEvent):void
{
btn_mute.btn_pause.visible = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void
{
btn_mute.btn_pause.visible = true;
myChannel = mySound.play(lastPosition);
}
And the mute function:
function setVolume(vol){
var volTransform:SoundTransform = new SoundTransform();
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
setVolume(0);

AS3 - my sound player seems to work well but the sound is not there

I've been doing a soundplayer for my assignment.Came across some issues. No compile error neither runtime error is appearing and my sound player functions well but I can't hear the song playing.
var mySound:Sound = new Sound ();
var myURL:URLRequest = new URLRequest ("playList.xml");
var mySoundChannel:SoundChannel = new SoundChannel;
var playing:Boolean = true;
var resumeTime:Number = 0;
var loadedXML:XML;
var mySongs:XMLList;
var myTotal:Number;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(myURL);
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void {
loadedXML = XML(e.target.data);
mySongs = loadedXML.SONG;
myTotal = mySongs.length();
}
btnPlayPause.buttonMode =true;
btnStop.buttonMode =true;
btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
function stopSound (f:MouseEvent):void {
mySoundChannel.stop();
}
Help is appreciated very much.
It seems like you trying to play sound only if it already playing, while stoping sound if it is NOT playing:
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
I see this code like this:
function playSound(){
if(playing){
mySound.play();
} else {
mySoundChannel.stop();
}
}
Maybe this is the root of problem

How to unmute the sounds in as3?

Sorry for my English..I have a code for two buttons to mute and unmute sounds..when I click to mute the sounds, it works but when I click to the other button to make the sounds to play it doesn't worked..any help?
import flash.media.SoundMixer;
speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound);
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound);
var channel2:SoundChannel = new SoundChannel();
var clicktoPlay:Boolean = true;
function speaker2sound(e:MouseEvent):void{
if (clicktoPlay==true){
var snd2:Sound = new Sound;
snd2.load(new URLRequest("shakeup.wav"));
clicktoPlay=false;
channel2 = snd2.play();
speakerb1.visible=true;
speakerb2.visible=false;
SoundMixer.soundTransform = new SoundTransform(1);
}
if (clicktoPlay==false){
clicktoPlay=true;
speakerb2.visible=true;
speakerb1.visible=false;
SoundMixer.soundTransform = new SoundTransform(0);
}
clicktoPlay =!clicktoPlay;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}
I got your code working with a few small changes:
speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound);
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound);
var channel2:SoundChannel = new SoundChannel();
var clicktoPlay:Boolean = true;
var snd2:Sound;
function speaker2sound(e:MouseEvent):void{
if (clicktoPlay==true){
if(!snd2){
snd2 = new Sound();
snd2.load(new URLRequest("Kalimba.mp3"));
channel2 = snd2.play();
}
speakerb1.visible=true;
speakerb2.visible=false;
SoundMixer.soundTransform = new SoundTransform(1);
}
else {
speakerb2.visible=true;
speakerb1.visible=false;
SoundMixer.soundTransform = new SoundTransform(0);
}
clicktoPlay =!clicktoPlay;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}
channel2.addEventListener(Event.SOUND_COMPLETE,soundfin);
function soundfin(event:Event):void{
clicktoPlay=false;
speakerb1.visible=true;
speakerb2.visible=false;
}
As Marcela says, try taking out the line "clicktoPlay =!clicktoPlay;" and see if it works.
The way your code is now, until "soundfin" is called, clicktoPlay will remain true no matter how often speaker2sound is called.

Streaming audio and video AS3

I have couple of videos that I'm streaming. I start with the video_1.flv and after it finishes the video_2.flv is running and looping until the user takes some action to play other movie for example video_3.flv. So I need when video_2.flv is playing in the background to be played audio file sound.mp3 witch is going to be looped , witch i managed to do. But I need both of them to run independently. Because right now when the video is looped the audio is looping to. And I need the audio to be playd only when video_2.flv is played. Thank YOU.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
loader.vid.Video_1.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_1.flv");
ns.addEventListener(NetStatusEvent.NET_STATUS, NCListener);
var clipTimer:Timer = new Timer(4000);
function NCListener(e:NetStatusEvent){
if (e.info.code == "NetStream.Play.Stop") {
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_2.flv");
sound.load(req);
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var req:URLRequest = new URLRequest("sound.mp3");
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play();
}
loader.button_01.addEventListener(MouseEvent.CLICK, play_video_01);
loader.button_01.addEventListener(MouseEvent.ROLL_OVER, play_effect_01);
function play_video_01 (event:MouseEvent){
clipTimer.stop();
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_3.flv");
loader.button_01_mc.gotoAndPlay (41);
}
Maybe this tutorial is helpful to you...
I'm new in AS but I'd rather prefer to pack the whole s.... stuff into a (reusable) class...
Behavior of a Loaded SWF File versus Behavior of a MovieClip
Greets
Gee