I’m trying to build a small app that will iterate through an array of sound files to see if they are valid.
I’m simulating corrupt sound files by taking .png’s and changing the extension on them to .mp3
The idea is a sound will start playing and the timer will start. When the sound is done playing the next sound will start and the timer will reset and start counting down again. If a sound does not finish within 45 seconds the timer will complete, output the name of the file that did not finish, and then skip to the next file.
For some reason though the timer does not reset when the sounds finish playing, it just keeps counting down from wherever it is.
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
var statusTextField:TextField = new TextField();
var timerTextField:TextField = new TextField();
var list:Array = new Array();
var snd:Sound = new Sound();
var channel:SoundChannel;
var delay:uint = 1000;
var repeat:uint = 45;
var myTimer:Timer = new Timer(delay,repeat);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
var currentFile:String;
var currentPercent:int;
list = ["validSound1.mp3","validSound2.mp3","corruptSound1.mp3","validSound3.mp3"];
statusTextField.autoSize = TextFieldAutoSize.LEFT;
timerTextField.autoSize = TextFieldAutoSize.LEFT;
timerTextField.y = 200;
addChild(timerTextField)
playNext();
stop();
function playNext():void
{
var currentSnd:Sound = new Sound();
var currentChannel:SoundChannel;
if (list.length > 0)
{
currentFile = list.shift();
var req:URLRequest = new URLRequest(currentFile);
try
{
currentSnd.load(req);
currentChannel = currentSnd.play();
myTimer.start();
}
catch (err:Error)
{
trace(err.message);
}
currentSnd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
currentChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
addChild(statusTextField);
snd = currentSnd;
channel = currentChannel;
}
}
function completeHandler(event:Event):void
{
trace(currentFile + " was at " + currentPercent + "% when the timer expired");
myTimer.reset();
playNext();
}
function timerHandler(e:TimerEvent):void
{
repeat--;
timerTextField.text = ((delay * repeat) / 1000) + " seconds left.";
}
function enterFrameHandler(event:Event):void
{
var loadTime:Number = snd.bytesLoaded / snd.bytesTotal;
var loadPercent:uint = Math.round(100 * loadTime);
var estimatedLength:int = Math.ceil(snd.length / (loadTime));
var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength));
currentPercent = playbackPercent;
statusTextField.text = "Sound file's size is " + snd.bytesTotal + " bytes.
"
+ "Bytes being loaded: " + snd.bytesLoaded + "
"
+ "Percentage of sound file that is loaded " + loadPercent + "%.
"
+ "Sound playback is " + playbackPercent + "% complete.";
}
function errorHandler(errorEvent:IOErrorEvent):void
{
statusTextField.text = "The sound could not be loaded: " + errorEvent.text;
trace(currentFile + " encountered a load error");
myTimer.reset();
playNext();
}
function soundCompleteHandler(event:Event):void
{
removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
myTimer.reset();
playNext();
}