by Jon (Updated on 2015-10-18)
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.
Import the clip into the Sound Editor by going to File > Create New > Sound.
Give it a name.
Tip: Alternatively, you can just drag and drop the sound in.
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.
Exercise: There's a flaw in this approach. Think about what happens when you re-enter this scene... That isn't desirable, is it?
Later in this article, we'll cover all the playback blocks available to you.
Stencyl accepts MP3s and OGGs as follows.
Platform | Accepted Format |
---|---|
Flash | MP3 |
HTML5 | OGG |
Windows | OGG |
Mac | OGG |
Linux | OGG |
iOS | OGG |
Android | OGG |
In short, we use OGG for every platform except for Flash. If you're publishing your game to Flash and any of the other platforms, you'll need to import the sound in both formats. (We recommend Audacity for exporting music. It’s free.)
If your game unexpectedly does not export your game to Flash, it’s likely that sounds are the culprit. Ensure that your MP3's meet the following specifications.
If you continue to experience issues with MP3s, post your issue to the forums and generate logs.
For Flash games, licensing is handled by Adobe (the creator of Flash), and as a game developer, you do not have to worry about licensing for your Flash game, even if it makes money.
For desktop and mobile games, we only work with OGG sounds, so the use of MP3's is irrelevant.
Stencyl supports two kinds of sounds: music and sound effects.
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.
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.
Note: If you’d like to pause and resume a sound, skip down to the Channels section.
Looping music does not stop upon switching scenes. A common mistake, however, is to attach a “Background Music” behavior to every scene, causing the music to restart each time you enter a new “room” or "stack up."
How would you solve this? One approach is to create a blank scene prior to the first level which loops the music. Since this scene is not encountered again, the loop behavior runs just once.
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.
Note: When you play a sound/music without specifying a channel, Stencyl picks the next available channel starting from 0. If none are available, nothing happens.
On the Android target, playing sounds may be delayed by a quarter to half second on some devices. This is due to a bug in Android's sound APIs. As of late 2015, this bug still exists and afflicts all products that use this API.
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.
How would you build this sound system?