I’m having some trouble getting Haxe to play audio files in Flash 8.
At the top of my hx file, I have:
import flash.MovieClip;
import flash.Sound;
and, within the class itself, I preload a lot of image files along with the names of the audio files.
The idea is to do a slideshow with audio content. Basically, display the first slide and play the audio associated with it.
Then, once that audio is finished, move on to the next slide and next audio file. I have the slides fading in and out okay but when I tried to add sound, nothing comes out the speakers.
The following code is what I’m doing – the sound file associated with audios[0] never starts playing and I’m at a loss as to why.
class Whatever {
static var master : MovieClip;
static var slides : Array<MovieClip>;
static var audios : Array<String>;
static var sound : Sound;
function new () {}
static function main () {
master = flash.Lib.current;
slides = new Array<MovieClip> ();
sound = new Sound (null);
var app : Whatever = new Whatever ();
var num : String;
var j : Int;
var clip : MovieClip;
// There are 12 pictures in this test, image[001-012].jpg.
// Each has an associated audioNNN.mp3 file.
for (j in 1...13) {
// Right-justify, zero fill.
num = "" + j;
if (j < 10) num = "0" + num;
if (j < 100) num = "0" + num;
// Load each image, hiding all but the first.
clip = master.createEmptyMovieClip ("clip_" + num, master.getNextHighestDepth());
clip.loadMovie ("image" + num + ".jpg");
if (j > 1) clip._alpha = 0;
slides.push (clip);
// Make another list of the audio files.
audios.push ("audio" + num + ".mp3");
}
// Start the first audio file.
sound.loadSound (audios[0], true);
}
}
Silly me!
It turns out that I just forgot to allocate the array before pushing values on to it. Why the runtime lets you do that without an error is a separate question.
All I had to do was change the code from:
to:
so that the audio array is created properly.
Regarding why the pushing to a non-existent array didn’t get caught by the runtime, there’s an interesting snippet in the Professional haXe and Neko book (which only arrived today and looks like it’s going to return my investment very quickly, which is why I don’t mind giving it a shameless plug) that explains it, pretty much exactly the situation I encountered: