Tuesday, April 6, 2010

Coding

I had a little fun working up a Flash system that snagged and XML file and an .MP3 file, mushed about inside of them and churns out a little soundtrack with text running along at the same time.

Unfortunately, I have no way of getting the XML to be smart enough to load up everything on the .MP3's timing. This just means that I have to use a tool (I use Audacity) to find the start points of each each text portion and tell the XML file where those are.

The tricky bit is that Flash doesn't have a way to reset its start timer. Not a huge deal, but doing the pause and resume stuff gets a little wacky while trying to keep tabs on the location on the .MP3 and the story itself.

The trick is to make sure to have a timer offset that you set when the story actually starts. And you set your start time to zero. Now you can compare safely, like:


timeElapsed = (timeElapsed + (getTimer() - timeOffset)) / 1000;
if (timeElapsed > storyTime)
{
....
}


When you pause, make sure to set a time paused variable using the getTimer(), and also pause the .MP3 and get its current position.


soundPausePoint = storySoundChannel.position;
storySoundChannel.stop();
timePaused = getTimer();


Then when you're ready to resume, you'll have all the junk you need to set your timers and your .MP3 start point accordingly.


storySoundChannel = storySound.play(soundPausePoint);
timeOffset = getTimer() + (timeOffset - timePaused);


Nifty, no?

1 comment: