I'm very bad with Flash and I just can't do this.
I have php generated playlist.
I get it with LoaderInfo(), then with split() the data is added to array.
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
var str:String = paramObj.playlist;
var array:Array = str.split(",");
Here is the problem. I know how to play 1 song from the playlist, once or in the loop with code like this:
sound.addEventListener(Event.COMPLETE, onLoadComplete, false, 0,
true);
function onLoadComplete(evt:Event):void {
channel = sound.play();
var pauseSong:Number = channel.position;
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function soundCompleteHandler(e:Event):void {
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
, but I cant make code to play in loop all songs in playlist -first, second, etc, first...
How to do that?
You make an array of your loading sounds, put a separate listener onto the first (0th) sound of your list that will initiate playback, all others should have another listener that will just notify your program that the sound is loaded. Once the first sound started playing, you add a listener to channel that will select next song and play it if it's available. If it's not available, your code should wait in a frame-based loop (Event.ENTER_FRAME listener with one single check) until it will finally load, and resume playlist playback.
Related
Trying to loop a background soundtrack while my flash program is in use.
So far my code is this:
//turn off sound
btnOff.addEventListener(MouseEvent.CLICK, fl_stopsound);
function fl_stopsound(event:MouseEvent):void
{
SoundMixer.stopAll();
}
//turns sound on
btnOn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound_1);
var fl_SC_1:SoundChannel;
//keeps track of whether the sound should be played or stopped
var fl_ToPlay_1:Boolean = true;
function fl_ClickToPlayStopSound_1(evt:MouseEvent):void
{
var mySound:Sound = new background();
mySound.play();
}
where btnOff turns off the sound and btnOn turns on the sound. My soundtrack is 1:50min long. Is it possible to loop the track within the program with these buttons?
Cheers
Use mySound.play(0, int.MAX_VALUE);
Goodmorning,
I'm working on a video flash player to stream. What I want to do, is to display the total time of the stream and not the time since the user is watching the stream.
I have a problem now, is that when I pause then play the video, the current time restarts.
Do you have any ideas to fix my problem and to solve the other one? :)
**I'm using NetStream
Alright, for the first problem, what you want to do is to setup a function that receives the MetaData of the video and save that value somewhere.
First, when you create your NetStream Object, you need to add a Client to the NetStream that references the function onMetaData.
var ns:NetStream; //your NetStream Object
var client:Object = new Object(); //Create an Object that represents the client
client.onMetaData = onMetaData; //reference the function that catches the MetaData of the Video
ns.client = client; //assign our client Object to the client property of the NetStream
//Once MetaData is available, it'll call onMetaData with all of the information
function onMetaData(metaData:Object):void
{
duration = metaData.duration; //duration is the variable that is supposed to total length of the video
}
Now with the duration value you get the total play time of the movie that is currently playing with that NetStream Object.
You can solve your second problem in a number of ways, for example:
pause() and resume()
pause() and player('currentTime')
Simply keep a Boolean Variable called pause that keeps track if the video is currently playing or not.
var paused:Boolean = false; //assuming the video is currently playing
var currentTime:Number = 0;
var button:Button; //some kind of play/pause button
button.addEventListener(MouseEvent.CLICK,onButtonClick);
function onButtonClick(event:MouseEvent):void
{
if(paused)
{
paused = false;
ns.resume();
//ns.play(currentTime) //this also works
}
else
{
paused = true;
ns.pause();
currentTime = ns.time;
}
}
You have to extract the information from the meta data of the file.
Thank you for you answers :).
DodgerThud, I tried your solution concerning my second problem this morning. I tried your solution and I've got this : duration : NaN. If I understand well, my problem is that I don't have the metadata of the stream...
if(isLiveStream == false) {
if(title == preroll || Number(duree) <= 0) {
duration=infoObject.duration+timeOffset;
} else {
duration=Number(duree);
}
}
Those lines are on the onMetaData function.
Thank you for helping me.
Have a nice day.
I have this 2 functions. And as you can see the first one is streaming a video (witch is looping ) and the second one is streaming an audio ( witch si looping too ). So I need when the video is playing the audio to starts running to and to be looped independently because the length of my audio is bigger than the length of the video. Any ideas how can i manage to do that. Use code please.
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");
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel= new SoundChannel();
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete );
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play(0, 9999);
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play(0, 9999);
}
First of all, you don't need the
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
It triggers when the sound has finished running, but since it's looping, there's no need to play it again.
First of all, you need to move the line
soundChannel = sound.play(0, 9999);
And place it right after your ns.play. So the sound will start as soon as your video start.
Then, to prevent it looping again when the video start, just check if soundChannel exists :
if (soundChannel == null)
soundChannel = sound.play(0, 9999);
Please notice that's there is no way to keep your sound in sync with your video afterwards, it depends completely on flashplayer will.
I am currently using the following code to play audio recordings from the project library.
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var sound1:Sound = new audio1();
var sound2:Sound = new audio2();
var mySoundChannel:SoundChannel = new SoundChannel();
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
}
//In this function, we create an argument that allows us to tell the function
//what sound to we want it to play.
function playSound(soundname:String):void
{
try
{
mySoundChannel = this[soundname].play(0, 0);
}
catch(error:ReferenceError)
{
trace("playSound: That sound name doesn't exist.");
return;
}
}
//Hook up buttons-->
button1.buttonMode = true;
button1.useHandCursor = true;
button2.buttonMode = true;
button2.useHandCursor = true;
button1.addEventListener(MouseEvent.CLICK, button1click);
button2.addEventListener(MouseEvent.CLICK, button2click);
function button1click(evt:Event):void
{
stopSound();
playSound("sound1");
}
function button2click(evt:Event):void
{
stopSound();
playSound("sound2");
}
I need to pause the currently playing audio when a button is clicked. How do I do this?
You will need to do five things to your current code in order to pause and resume the currently playing sound:
Create a variable that stores the name of the currently playing
sound, and a variable to store the pause position of the audio.
Create a pause function.
Create a resume function.
Expand the current playSound and stopSound functions to work with the new variables.
Hook up the button event listener.
Step 1:
var currentSound:String = "";
var pausePosition:Number = 0;
Step 2: We're going to save the current position of the audio in that second variable we just created. We can get the current play position of the audio using the mySoundChannel.position property, which returns a Number value (matching the Number type we gave the pausePosition variable).
function pauseSound():void
{
//If there's a song to pause...
if(currentSound != "")
{
//Get pause position.
pausePosition = mySoundChannel.position;
//Stop the sound directly.
mySoundChannel.stop();
}
}
Note we didn't call stopSound(). There's a good reason for that. We're going to put an extra line of code in that function shortly that we don't want to use in pauseSound().
Step 3: Now we create the function to resume audio. Note this is NOT the same as playSound(). We're telling it to start playing from pausePosition, not from 0 (the beginning of the sound clip).
function resumeSound():void
{
//If there's a song to resume...
if(currentSound != "")
{
//Start playing the current audio from the position we left off at.
mySoundChannel = this[currentSound].play(pausePosition);
}
}
Step 4: Since we're now working with those variables we declared in step 1, we need to adjust how playSound() and stopSound() work.
In playSound(), instead of just passing soundname to the sound channel, we're going to save the soundname to currentSound.
function playSound(soundname:String):void
{
try
{
currentSound = soundname
mySoundChannel = this[currentSound].play(0, 0);
}
catch(error:ReferenceError)
{
trace("playSound: That sound name doesn't exist.");
return;
}
}
In stopSound(), we need to actually clear the currentSound and pausePosition variables when we stop, to ensure that resumeSound doesn't start audio after we've totally stopped it.
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
//Clear our variables.
currentSound = "";
pausePosition = 0;
}
GOTCHA WARNING: Ordinarily, you can loop audio by passing an integer other than 0 in the second argument (where I have the 5) in the following code:
mySoundChannel = this[currentSound].play(0, 5);
In the above code, the sound would play from the beginning, and repeat five times.
However, if you are starting the audio from any position other than 0, what will actually happen is that the audio will loop at the position you start at, not at the beginning.
That is to say, if you use this code:
mySoundChannel = this[currentSound].play(1000, 5);
The sound will loop five times, but every time the sound starts over in the loop, it will start playing from position 1000, and NOT the beginning of the sound (0).
I hope that answers your question!
Using ONE Sound() object in Actionscript3, how can I play a one MP3 and then, when the user chooses a different one, play a second sound, using the SAME Sound() object?
EDIT: See my answer for how I did it.
You cannot use same Sound object to play multiple files.
Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.
Ok, I actually did it using the following code. My bug was somewhere else in the FLA file, but this works. I made an uninitialized global variable and created the Sound() object LOCALLY inside of a function. While I'm technically using multiple sound objects, my references are all pointing to ONE object. Additionally, I can call these methods over one another for easier coding. This works for me:
/* -------------
Sound player
functions
------------ */
var snd:Sound; //the sound object
var sndC:SoundChannel; //the soudchannel used as "controller"
var sndT:SoundTransform; //soundTransform used for volume
var vol:Number = 1; //the volume of the song
var pan:Number = 0; //panning of the sound
var pos:Number = 0; //position of the song
var currentSound:String; //currently playing song?
function playSound(s:String){ //this function resets the sound and plays it
stopSound(sndC); //stop the sound from playing
snd = new Sound(); //reset the sound
snd.load(new URLRequest(s)); //load the desired sound
sndC = new SoundChannel(); //(re-)apply the sound channel
applyVolume(vol,pan,sndT,sndC); //apply the volume
sndC = snd.play(pos); //play it
sndC.addEventListener(Event.SOUND_COMPLETE, startSound); //remind it to restart playing when it's done
} //end function
function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){ //takes an argument for the volume, pan, soundTYransform and soundChannel
sndT = new SoundTransform(n,p); //applies the soundTransfrom settings
sndC.soundTransform = sndT; //and attaches it to the soundChannel
} //end function
function stopSound(sndC:SoundChannel){ //this function stops a sound from playing
if(sndC != null){ //if the sound was used before (ie: playing)
if(currentLabel == "video-frame"){ //if we are in the video frame
pos = sndC.position; //store the position of the song to play from at a later time
}else{ //otherwise
pos = 0; //set the position at 0
} //end if
sndC.stop(); //stop it
} //end if
} //end function
function startSound(snd:Sound){ //restarts a sound when it's playing
if(snd != null){ //if the sound exists
sndC = snd.play(pos); //play it
} //end if
} //end function