Chuck
Chuck

Reputation: 375

AS3 Start and stop audio from the middle of audio track

I have a single audio file, I'd like to avoid cutting it up.

I know I can use the sound class, mySound.play(150), to start at 150ms but haven't come across a way to stop the audio say after 500ms or at 650ms.

Thanks for any input.

Upvotes: 3

Views: 1806

Answers (2)

Corey
Corey

Reputation: 5818

You could use an enter frame event to check the SoundChannel position - SoundChannel. Once the position is greater than or equal to your timecode, stop the Sound. This of course won't be incredibly accurate and will depend on the frame rate of the application.

var mySound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();

addEventListener(Event.ENTER_FRAME, loop);
channel = mySound.play(150);

function loop(e:Event):void {
   if (channel.position >= 500) {
      channel.stop();
   }
}

Upvotes: 2

taskinoor
taskinoor

Reputation: 46027

Unfortunately there may not be any good method to do this. One work around might be to create a timer and stop when the timer handler is fired. Something like this:

sndChannel = mySound.play(200);
var stopTimer:Timer = new Timer(500, 1);   // stop after 500ms
stopTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onStopTimer);
stopTimer.start();

private function onStopTimer(evt:TimerEvent):void {
    sndChannel.stop();
}

Really not a very good and precise solution, but this may work in practice.

Upvotes: 0

Related Questions