Refresh stream content - actionscript-3

When pres play it connects to stream and its all ok!
When I click pause btn it stops sound, but the problem
is when you click play btn after them it starts playing stream from the same place where you stop it, doing this all time nothing happens it always start stream from the same place when do it 1st time...
play_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
pause_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
spiner_mc.visible=true;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
spiner_mc.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}

everything was easy!
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}
The s:Sound, req, context and s.load cant be placed outside and thats all...

Related

Volume control after loop as3

I have a problem with my code. The code works great with one exception, if I have pressed the mute-button and the animation loops, it's always back to volume = 1. How can it loop without always going back to volume = 1?
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
volumeAdjust.volume = 1;
mySound.load(songURL);
channel1.soundTransform = volumeAdjust;
channel1 = mySound.play();
function muteLyd(e:MouseEvent):void {
if(volumeAdjust.volume == 1){
volumeAdjust.volume = 0;
}
else {
volumeAdjust.volume = 1;
}
channel1.soundTransform = volumeAdjust;
}
Just found out this code solves the problem:
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
unmute1.addEventListener(MouseEvent.CLICK, unMuteLyd);
mySound.load(songURL);
channel1 = mySound.play();
function muteLyd(evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(0);
unmute1.visible = true;
mute1.visible = false;
}
function unMuteLyd (evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(1);
unmute1.visible = false;
mute1.visible = true;
}

AS3 Play & Pause Current (loaded) MP3 (Flash CC)

I have 8 external mp3s that are loaded and will be played sequencially. I cannot figure how to pause the mp3 currently playing and start playing from the point it was paused. I did have code that would pause it but when clicking the play button, it would play the first mp3 from the beginning.
var s1:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var s2:Sound = new Sound(new URLRequest("Audio Files/2.mp3"));
var s3:Sound = new Sound(new URLRequest("Audio Files/3.mp3"));
var s4:Sound = new Sound(new URLRequest("Audio Files/4.mp3"));
var s5:Sound = new Sound(new URLRequest("Audio Files/5.mp3"));
var s6:Sound = new Sound(new URLRequest("Audio Files/6.mp3"));
var s7:Sound = new Sound(new URLRequest("Audio Files/7.mp3"));
var s8:Sound = new Sound(new URLRequest("Audio Files/8.mp3"));
s1.addEventListener(Event.COMPLETE, doLoadComplete);
s2.addEventListener(Event.COMPLETE, doLoadComplete);
s3.addEventListener(Event.COMPLETE, doLoadComplete);
s4.addEventListener(Event.COMPLETE, doLoadComplete);
s5.addEventListener(Event.COMPLETE, doLoadComplete);
s6.addEventListener(Event.COMPLETE, doLoadComplete);
s7.addEventListener(Event.COMPLETE, doLoadComplete);
s8.addEventListener(Event.COMPLETE, doLoadComplete);
var channel:SoundChannel = new SoundChannel();
channel = s1.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
trace("1 done.");
channel = s2.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete2)
}
function doSoundComplete2($evt:Event):void
{
trace("2 done.");
channel = s3.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete3);
}`
Here is what I have so far:
This loads the mp3s and plays them. The pause btn works but the play button to resume the audio gives me an error : ReferenceError: Error #1069: Property 0 not found on flash.media.Sound and there is no default value. at mp3sequence_fla::MainTimeline/playSound()
My guess is that the value for the current position or last position is incorrect.
var myArray:Array=[0,1,2,3,4,5,6,7];
var i:uint=1;
var req:URLRequest = new URLRequest("mp3/"+myArray[i]+".mp3");
var VSound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; //last position of the sound
var curSoundIndex:int = 0; //var to store the current sound that is playing
VSound.load(req);
channel = VSound.play();
function playSound(e:Event = null) {
//if no sound channel, load the current sound into it
channel = VSound[curSoundIndex].play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true);
lastPosition = channel.position;
}
function pauseSound(e:Event = null) {
if (channel) {
lastPosition = channel.position;
channel.stop();
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= VSound.length) curSoundIndex = 0;
}
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
To pause a sound you can store it's position when you pause it, and use that variable to replay from that position.
var s:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var channel:SoundChannel = new SoundChannel();
channel = s.play();
var pausePosition:int;
function pause():void {
soundPosition = channel.position;
channel.stop();
}
function resume():void {
channel = s.play(soundPosition);
}
You can store the position property of the corresponding SoundChannel. I've added some code to make the whole thing less redundant
var curSoundIndex:int = 0; //var to store the current sound that is playing
var lastPosition:Number = 0; //last position of the sound
var soundChannel:SoundChannel;
var sounds:Vector.<Sound> = new Vector.<Sound>(); //array of all sounds
loadNextSound();
function loadNextSound(e:Event = null):void {
//check if all sounds are loaded
if (sounds.length >= 8) {
curSoundIndex = 0; //select first sound
resume(); //start playing
return;
}
//if not, load the next sound and add it to the array/vector
var sound:Sound = new Sound(new URLRequest("Audio Files/" + (sounds.length + 1) + ".mp3"));
sounds.push(sound);
if(sound.bytesLoaded < sound.bytesTotal){ //check if already loaded
sound.addEventListener(Event.COMPLETE, loadNextSound);
}else{
loadNextSound();
}
}
function pause(e:Event = null) {
if (soundChannel) {
lastPosition = soundChannel.position;
soundChannel.stop();
}
}
function resume(e:Event = null) {
//if no sound channel, load the current sound into it
soundChannel = sounds[curSoundIndex].play(lastPosition);
soundChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true); //use weak listener to avoid memory leaks
lastPosition = 0;
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= sounds.length) curSoundIndex = 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

AS3 - Remove Child Problem

I'm using the following code to display an advertisement in a mobile game that I'm working on. It works fine but I don't know what to put in the clickStart function to remove the advertisement from the stage before the game plays. I've been playing around with removeChild but can't seem to get it to work.
stop();
startButton.addEventListener(MouseEvent.CLICK,clickStart);
function clickStart(event:MouseEvent) {
gotoAndStop("play");
}
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
l.x = 135;
l.y = 265;
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
You need to move the declaration of the Loader outside of the onComplete function, since the variable runs out of scope.
var l:Loader = new Loader();
You could try placing it on the same scope as these:
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
then you should be able to update your clickStart function to something like this:
function clickStart(event:MouseEvent) {
removeChild(l);
gotoAndStop("play");
}

AS3 - positon advertisment that is dynamically loaded

I'm using the code below to place an ad inside a mobile game that I'm building. My problem is that the ad appears in the top left corner of the stage and I need to be able to move it centered near the bottom. I got this code from a book and I understand about 70% of what it's doing. Thanks for any direction you can offer.
Rich
// Smaato Advertising Code for Start Screen
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
The problem is that you are never actually positioning the image, so the default is [0,0], the top-left corner of the screen.
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onAdLoaded);
function onAdLoaded(e:Event):void
{
var ad:DisplayObject = e.target.content;
addChild(ad);
ad.x = (ad.stage.stageWidth - ad.width) / 2;
ad.y = (ad.stage.stageHeight - ad.height) / 2 + 50;
}
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
That will center the loaded file. The 50 number is because if you don't want it to be in the exact center, you can use the number to adjust the y-value accordingly.
By the way, I recommend you to grab another book, becase the code you are using seems a bit old and have poor performance.
If you have any other issue regarding this, comment it and I can expand my answer.