Wednesday, October 16, 2013

How to load audio/sound (.mp3) files with Starling Framework?

Sound effect plays an important role in any game. It stimulates the players to immerse themselves into the games. It manipulate the game paces, and the players' heart beat.

Similarly, an Android game without any background music or sound effect won't keep the players long. Hence, it is important to know how to load audio/sound files into your game, playing them at a correct timing. Here, the codes for doing it with Starling Framework will be displayed.

1. Import Flash's Sound library

It is alright to just use the Sound library from the Flash. In fact, there isn't any specific library for the sound in Starling Framework.

import flash.media.Sound;
   

2.  Embed the audio/sound file

Like embedding the image files, embedding an audio/sound is similarly straightforward.

[Embed(source="../media/sounds/move.mp3")]
public const MOVE_SOUND:Class;

 3. Define the sound variable     

Here, note that it is necessary to use "as Sound" when defining the variable.

public var sndMove:Sound = new MOVE_SOUND() as Sound;
       

4. Play the audio/sound file

 With the play function, you can directly play the audio file, with customizable parameters. (specify start time, loop times, sound transform)

 sndMove.play()

3 comments: