-1

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);
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
DMC66
  • 3
  • 3
  • Was there a problem with the answer? – BadFeelingAboutThis Oct 28 '14 at 15:56
  • Hi LDMS. I thought this was working. I didn't realize that I had my original code buried within an MC that was loading the mp3s and making it appear all the mp3s were playing in sequence. Once I removed this and used just your code, only the first mp3 is played. – DMC66 Oct 29 '14 at 15:07

2 Answers2

1

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);
}
Karmacon
  • 3,128
  • 1
  • 18
  • 20
  • didn't mean to rip off your answer, took me a while to type mine up and yours came in during that process. I'm going leave mine though because it addresses some of the OP's other issues. – BadFeelingAboutThis Sep 19 '14 at 00:06
  • @LDMS No worries. Your answer is far more complete. I thought about going the whole nine yards too, but opted with just answering the question and moving on :P – Karmacon Sep 19 '14 at 00:44
  • Thx Karma. Your code is what I had started out with. I could not figure how to target the mp3 currently playing. I appreciate your help though. thx! – DMC66 Sep 19 '14 at 14:06
0

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;
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • thx LDMS. I'm asked to develop projects like this once every 12 to 18 months. I have to re-learn AS3 all over again. :) I will try your code and post my result. Thanks for the help. – DMC66 Sep 19 '14 at 14:11
  • After setting up the pause and resume buttons I get this error when I click to pause and resume. ArgumentError: Error #1063: Argument count mismatch on flow_test_fla::testMC/resume(). Expected 0, got 1. – DMC66 Sep 19 '14 at 16:41
  • Just add `e:Event = null` as an argument to the resume and pause functions if calling it from an event listener (like a button click) – BadFeelingAboutThis Sep 19 '14 at 16:44
  • Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error. - I'll try and work this one out. We'll see how long before I come running back here. – DMC66 Sep 19 '14 at 18:57