Skip to main content
Known Participant
February 28, 2014
Question

Control playhead location with sound array

  • February 28, 2014
  • 3 replies
  • 1605 views

Hey Everyone,  Have some old AS2 that I need to transition over to AS3.  I am having trouble finding reference on how to convert the array portion of the AS2 code below.  We used this to control the playhead so we could bring things onto the screen times to certain parts of our audio. I have gotten my audio playing with the AS3 at the bottom of this message but can't seem to tie it into an array.  Any auggestions or things you can point me to?

//AS2

//placed on Frame 1

soundArray = new Array();

soundArray = [8,15,21,27,36];

trace(soundArray);

speech = new Sound(this);

//placed on Frame2

stopAllSounds();

counter = 0;

_root.sndPause = 0;

silent = false;

//set to true onSoundComplete

speech.loadSound("audio/1307_t2_p5.mp3", true);

speech.start();

//playMC.gotoAndStop(2);

speech.onSoundComplete = endLoop;

// This code was placed on Frames where I "held" the playhead until another array point was met

stop();

syncPoint = (soundArray[counter]*1000);

ID = setInterval(this, "isDone", 500);

//////////////////////////////////////////////////////////////////////////////

//AS3

import flash.events.Event;

import flash.media.Sound;

import flash.net.URLRequest;

var speech:Sound = new Sound();

speech.addEventListener(Event.COMPLETE, onSoundLoaded);

var req:URLRequest = new URLRequest("audio/993_m2_p6.mp3");

speech.load(req);

function onSoundLoaded(event:Event):void

{

    var localSound:Sound = event.target as Sound;

    localSound.play();

}

This topic has been closed for replies.

3 replies

lickteigAuthor
Known Participant
February 28, 2014

dang....forgot to include the "isDone" portion.  It's sit on Frame 2 after "speech.onSoundComplete = endLoop;"

function isDone() {

    if (speech.position>syncPoint) {

        clearInterval(ID);

        counter++;

        play();

    }

}

Inspiring
February 28, 2014

Are you simply using the array as a timing mechanism and calling the isDone() function every 1/2 second to check? If so, just use a Timer in AS3 - the array stuff should be identical. Also, in your AS3 code, why are you making a new localSound variable within your onSoundLoaded? In there you can just call speech.play() - you already have a reference to it, why not use it. I'm not fully sure what you are doing, but if you are just wanting to do something at specific times then I think this should be useful:

var soundArray:Array = [8, 15, 21, 27, 36];

var counter:int = 0;

var syncTimer:Timer = new Timer(0, 1);

syncTimer.addEventListener(TimerEvent.TIMER, nextSync, false, 0, true);

var speech:Sound = new Sound();

speech.addEventListener(Event.COMPLETE, onSoundLoaded);

var req:URLRequest = new URLRequest("audio/993_m2_p6.mp3");

speech.load(req);

function onSoundLoaded(event:Event):void

{

   speech.play();

   counter = -1;

   waitForSync();  

}

function waitForSync():void

{

          syncTimer.reset();

          counter++

          if(counter == 0){

                    syncTimer.delay = soundArray[counter] * 1000;

          }else{

                    syncTimer.delay = (soundArray[counter] - soundArray[counter - 1]) * 1000;

          }

          syncTimer.start();

}

function nextSync(e:TimerEvent):void

{

          //do sync stuff here...

          trace("sync at:", soundArray[counter]);

          waitForSync();

}

Also, just to mention you can use Adobe Media Encoder to place cuepoints in the sound file and listen for those...

lickteigAuthor
Known Participant
February 28, 2014

I am just wanting to move the playhead at specific times during audio playback.  So for example as the audio plays I can display items in a bulleted list (without having to speread things out on the timeline)  or show and image at a certain time and so on.  That's where the SoundArray comes in "soundArray = [8,15,21,27,36];"

Inspiring
February 28, 2014

OK, then I think this should work:

Frame 1 code:

var soundArray:Array = [8, 15, 21, 27, 36];

var counter:int = 0;

var syncTimer:Timer = new Timer(0, 1);

syncTimer.addEventListener(TimerEvent.TIMER, syncReady, false, 0, true);

var speech:Sound = new Sound();

speech.addEventListener(Event.COMPLETE, onSoundLoaded);

var req:URLRequest = new URLRequest("audio/993_m2_p6.mp3");

speech.load(req);

function onSoundLoaded(event:Event):void

{

   speech.play();

   counter = -1;

   waitForSync();  

}

function waitForSync():void

{

          syncTimer.reset();

          counter++

          if(counter == 0){

                    syncTimer.delay = soundArray[counter] * 1000;

          }else{

                    syncTimer.delay = (soundArray[counter] - soundArray[counter - 1]) * 1000;

          }

          syncTimer.start();

}

function syncReady(e:TimerEvent):void

{

          play();

}

Frames where you want to wait for the next sync:

stop();

waitForSync();

You'll need to add some error checking, but hopefully this gets you in the right direction.

kglad
Community Expert
Community Expert
February 28, 2014

what's the isDone function?