tags:

views:

36

answers:

1

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);
    }
}
+2  A: 

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:

slides = new Array<MovieClip> ();

to:

slides = new Array<MovieClip> ();
audios = new Array<String> ();

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:

So, now that you can see what you're up against, take a look at both Neko and Flash when generating an exception:

class UncaughtException {
    public static function main() {
        var t : Array <String> ;
        t.push("me");
    }
}

Compile the preceding class for both Neko and Flash, and then run them both. When run, the Flash player should display a blank screen, while the Neko application will generate the following text:

Called from  line 1
Called from UncaughtException.hx line 6
Uncaught exception - Invalid field access : push

This is an uncaught exception, which means it is an exception that you have not caught and dealt with in your code. The problem with the preceding class is that the Array t was not instantiated before the method push was called, so as far as the virtual machine is concerned, there is no method called push available.

Now, the fact of the matter is, the Flash virtual machine would have hit the same wall that the Neko virtual machine encountered, except that the Flash virtual machine chose to ignore the error and continue as normal. Does this mean the exception was even generated? Maybe, but, like most invisible pests, you'll need to catch one to prove it exists.

paxdiablo