Playing Sounds & Music
by Jon (Updated on 2014-02-20)
Sound is a fundamental aspect of almost any game. This section covers the basics of importing music, playing it back and some interesting tidbits that may not be obvious.
- Importing Sounds
- Example: Background Music
- Accepted Formats
- Music vs. Sound Effects
- Playing Sounds
- MP3 Licensing (Not an Issue!)
- Known Issues
- The Future
- Challenge: Zelda-Style Battle Music
- Challenge: Smooth Looping
How to: Importing Sounds & Music
1) Import music into the Sound Editor by going to File > Create New > Sound.
2) Give it a name.
3) Click the following box to pick and import.
Example: Looping Background Music
Looping "background" music takes just 1 block to do.
Make a Scene Behavior out of this, or stick this directly into the "Events" for a scene.
Later in this article, we cover all the playback blocks available to you.
Starting with Stencyl 3, Stencyl accepts MP3s and OGGs.
For Flash games, the MP3s are used. For mobile and desktop games, OGGs are used. If you're publishing your game to both categories, you'll need to import in both formats.
If possible, import MP3’s in the following format.
- 44.1 KHz (versus 22 or 11)
- Constant bitrate (versus VBR)
- No metadata
If your game unexpectedly does not export your game to Flash, it’s likely that sounds are the culprit. Post your issue to the forums and generate logs. (TODO: Take this note out and tell users how to solve it on their own.)
Why can't Stencyl standardize on just one format?
Due to limitations in our underlying technology (OpenFL), we are unable to standardize on one format. Flash playback of OGG music is not available at this time.
Music vs. Sound Effects
You may have noticed the following dropdown inside the Sound Editor. What does it mean?
Music is streamed (like viewing a YouTube video) since it’s too large to fit into memory. This is best for background music. This incurs a small performance penalty on mobile devices.
Sound effects are loaded into memory to reduce latency in playback. This is better for short clips that need to be played immediately but consumes memory, particularly on mobile devices.
How to Play Sounds & Music
All sound-related blocks are conveniently located under the Sound category.
Play = Plays to the end once, then stops
Loop = Plays to the end and then repeats
Volume ranges between 0% and 100%, inclusive.
One approach is to create a blank, starting scene prior to the first level which loops the music.
Suppose that you’re playing Zelda. The regular tune plays, but when you approach an enemy, and the music switches over to the battle music. Once you defeat the enemy, the regular tune picks up where it left off.
Channels are a simple way to refer back to a playback instance of sounds, so that you can control their volume and pause/resume them in the future.
Channels are referred to by number, starting at index 0, ending at index 31.
32 channels are available in total.
MP3 Licensing (Not an Issue)
On occasion, we get questions about MP3 Licensing. In short, there's absolutely nothing to worry about.
For Flash games, licensing is already handled by Adobe (the creator of Flash), and as a game developer, you do not have to worry about licensing for your Flash game.
For desktop and mobile games, we only work with OGG sounds, so you are in the clear.
We'd like to standardize on a single music format (OGG) and would like to support sequenced formats such as MIDI and tracker music (XM, IT).
On top of that, we’d like to add more control over playback, such as panning, altering pitch, seeking to a certain point in a clip and more.
Known Issues (as of Stencyl 3.0)
- On the Android target, playing sounds may be delayed by a quarter to half second on a small subset of devices, including the Galaxy S4. This is due to a bug in the underlying technology. We hope that this will be fixed in the future.
- Music = Streamed, Sound Effects = Loaded into Memory
- Channels let you control playback. They have little to do with the regular meaning of sound channels.
- Don’t worry about MP3s - Adobe has you covered. On other platforms, we now use OGGs.
Challenge: Zelda-style Battle Music
How would you build this sound system?
Challenge: Smooth MP3 Looping
MP3's can sometimes have a hard time looping smoothly and seamlessly. Some pieces of software are able to mask this or work it out. One of our users posted up an interesting link talking about a guaranteed-way of creating gapless MP3's.
Your task is simple - run through it and see if it works for you. It's a long article, and if you are able to boil that down to a smaller series of steps, we've love to know about it (in the comments section here!)