• Keep in touch:
  • Linked In
  • Twitter
  • RSS

How do I add a sound to a movie?

Playing a sound in a movie is very simple. Use the SoundFactory class to generate the sound definition and then add an instruction to the Flash Player to start playing it.

final SoundFactory factory = new SoundFactory();
factory.read(new File(soundFile));
final DefineSound sound = factory.defineSound(identifier);

movie.add(new StartSound(new SoundInfo(identifier,
    SoundInfo.Mode.START, 0, null)));

Slightly confusingly, the StartSound is also used to stop a sound once it is playing.

movie.add(new StartSound(new SoundInfo(identifier,
    SoundInfo.Mode.STOP, 0, null)));

Here SoundInfo class is used to simply start and stop a sound however it can also be used to control how a sound is played: defining whether it fades in or fades out, the number of times the sound should be played or even defining an envelope for the sound that can be used to control the volume at any point. Hence the 0 and null arguments respectively.