updated 27 sept 2002 | brown | green | blue | grey
daryl croke
research diary
edim 2002

Week 11:

Building a better audio console in Flash

In week 9 an audio console was developed in Flash using the getTimer() function. Although the console worked it was found to contain some bugs.

These problems resulted from the rather complicated mathematics involved with tracking time since the movie started and using that to control audio. A cleaner method would be track the time position of a playing audio file. It was wrongly assumed that Flash didn’t have a function to track this.

Sound objects in Flash have property called "postion". Therefore mySound.postion returns the "number of milliseconds the sound has been playing." A pause button could be built that stops the sound and returns assigns mySound.postion to a variable that in turns controls the start time of the sound function. I used this method to build a better sound console in flash.

Pause button stops sound and returns sound position (time in milliseconds). We need to divide this figure by one thousand to return seconds.

pause_button.onRelease = function(){
		timing = false;	
		display = mySound.position/1000;
		mySound.stop();
}

Start time of sound controlled by the variable "display".

function playSound() {
      if(timing !=true) {
      	mySound = new Sound(this);
      	mySound.attachSound("narration");
      	mySound.start(display,0);
      	}
 }

Stopping and rewinding the sound to the start is simply a matter of making "display=0".

rewind_button.onRelease = function () {
      		timing = false;
      		mySound.stop();
      		display=0;
 }

To automatically stop and rewind when the sound is finished we use another sound property, duration. Duration returns the length in milliseconds of the sound file. Therefore mySound.duration can be used to determine when the audio has finished playing. In the script below when the mySound.duration = mySound.postion (end of song) the display is set to zero. When the play button is pushed again the audio will play from the start.

_root.onEnterFrame = function() {
	if(timing) {
			display = mySound.position/1000;
			sampleLength = mySound.duration/1000;
	
			if(display>=sampleLength) {
				display = 0;
				timing = false;
			}
		}
}

The script on the play button simply calls the playSound() function.

play_button.onPress = function() {
		playSound();
		timing = true;
}

I have added true / false conditions on the variable display. This has a dual purpose. One it controls whether the output is dynamic displayed on the stage. Two it prevents audio re-triggering if it’s already playing.

New Flash sound console loaded with preloader