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;
}
Related
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;
}
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...
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
This is a very broad question , can I de-crpypt a streaming file, (assuming that i encrpyting this file on the server side during upload) , in flex before it being processed for playing.
You can process sound bytes before playing. This is example from Adobe documentation:
var sourceSnd:Sound = new Sound();
var outputSnd:Sound = new Sound();
var urlReq:URLRequest = new URLRequest("test.mp3");
sourceSnd.load(urlReq);
sourceSnd.addEventListener(Event.COMPLETE, loaded);
function loaded(event:Event):void
{
outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
outputSnd.play();
}
function processSound(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
sourceSnd.extract(bytes, 4096);
event.data.writeBytes(upOctave(bytes));
}
function upOctave(bytes:ByteArray):ByteArray
{
var returnBytes:ByteArray = new ByteArray();
bytes.position = 0;
while(bytes.bytesAvailable > 0)
{
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
if (bytes.bytesAvailable > 0)
{
bytes.position += 8;
}
}
return returnBytes;
}
Refer this link.
I am creating a sound mixer in AS3. I have several sound samples that a user can choose from to create their own track. The mixer will "mixdown" a max of 4 tracks and playback the "mixed" track on demand. I'm having trouble getting the sounds to playback in sync. I'm new to working with sound in flash and wondering what I'm missing... Here is the "mix" function at present:
function mixIt(e:MouseEvent) {
if (isPlaying) {
channel1.stop();
}
var mChannel1:SoundChannel = new SoundChannel;
var mChannel2:SoundChannel = new SoundChannel;
var tUrl1:URLRequest = new URLRequest(trackChoice1);
var tUrl2:URLRequest = new URLRequest(trackChoice2);
var s1:Sound = new Sound();
var s2:Sound = new Sound();
var s1Done:Boolean = false;
var s2Done:Boolean = false;
s1.addEventListener(Event.COMPLETE, onSoundLoaded1);
s2.addEventListener(Event.COMPLETE, onSoundLoaded2);
s1.load(tUrl1);
s2.load(tUrl2);
function onSoundLoaded1(e:Event) {
s1Done = true;
if (s2Done) {
playMix()
}
}
function onSoundLoaded2(e:Event) {
s2Done = true;
if (s1Done) {
trace("s2 Done")
playMix()
}
}
function playMix(){
mChannel1 = s1.play(0, 2);
mChannel2 = s2.play(0, 2);
//trace("MIXING TRACKS :" + tUrl1 + " + " + tUrl2);
}
}
try mixing sounds as byte arrays using Sound.extract() and SampleDataEvent