Creating a Blackberry Game - Part 6

Looking for part 5?

Let's Hear It!

Our game is almost done - the only part left is the sound (and vibration) processing. This is a fairly simple class as well - again, most of the low level processing is done already by the Blackberry. However - you may want to play with the methods in this class. We have functionality for playing a midi file - and we could also have functionality for playing a wav file as well, but I haven't included it. The reason - on my 8830, the sound engine (at least the way I was using it), could only seem to mix one sound at a time, and completely stopped the other sound if a second one was played. There may be specific methods to mix two sounds together that I did not research, or otherwise it's a limitation/bug of the 8830. So, if music was playing, and I then played a wav sound effect, the music would stop.

I overcame this by using the Alert.startAudio method. This takes frequency/duration pairs from an array and plays simple sounds with it. When this method is used, it does indeed mix the audio with the midi playing in the background, so I stuck with it. It makes for less sophisticated sound effects, but it helps us for now.

Additionally, I wanted to include vibration in the game, so I included a small method for triggering this off. Vibration is used for when spaceships explode, either yours or the enemy's.

SND.java

 
package com.synthdreams.GalacticBlast;
 
import net.rim.device.api.ui.component.Dialog;
import java.io.InputStream;
import java.lang.Class;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import net.rim.device.api.system.Alert;
 
// Sound engine
class SND {
 
    Player _musicPlayer; // Java media player
 
    SND() { }
 
    // Play a midi file for background music
    void playMusic(String passMusic)
    {
        try
        {
            // Set InputStream to a midi file included as resource, as specified by
            // passMusic
            InputStream in = getClass().getResourceAsStream("/" + passMusic);            
 
            // Create a media player with mime type of audio/midi using our inputstream
            _musicPlayer = javax.microedition.media.Manager.createPlayer(in, "audio/midi");
 
            // Ready the data and start playing it.  To loop indefinitely, we set loopcount
            // to -1.
            _musicPlayer.realize();
            _musicPlayer.prefetch();
            _musicPlayer.setLoopCount(-1);
            _musicPlayer.start();
 
        }
        catch (Exception e)
        {
            Dialog.alert("Error playing music");
        }
    }
 
    // Stop playing music
    void stopMusic()
    {
        try
        {
            // Tell player to stop playing
            _musicPlayer.stop();
 
        }
        catch (Exception e)
        {
            Dialog.alert("Error stopping music");
        }
 
        // Then release the data and close out the player
        _musicPlayer.deallocate();
        _musicPlayer.close();
    }
 
    // The Playsound method plays a simple combinations of tones to simulate a firing
    // noise.  This was necessary, as due to a bug or limitation of the BlackBerry 8830
    // (the phone I do my testing on), playing a WAV file stopped the midi player and
    // any other sound effects.  Player doesn't appear to mix properly (if at all).  However,
    // a midi file can be played while using the Alert objects startAudio method which
    // can play a sequence of tones, so this is what we've done for now.
    void playSound()
    {
        // A sequence of frequencies and durations (eg 1400hz for 15ms, 1350hz for 15ms, etc)
        short[] fire = {1400, 15, 1350, 15, 1320, 20, 1300, 20, 1250, 25, 1200, 35};
 
        try
        {
            Alert.startAudio(fire, 100);
 
        }
        catch (Exception e)
        {
            Dialog.alert("Error playing sound effect.");
        }   
 
    }
 
    // Activates the phone's vibration functionality for a specific number of ms
    void vibrate(int passMilli)
    {
        Alert.startVibrate(passMilli);
    }
}
 

Nothing too complex at all going on here. You may wonder what all the player initialization methods are doing - they deal mainly with making sure the sound data is available and buffered before playing. Just call them in order and you're good to go.

That's All Folks!

At this point, you have all the basic functionality necessary for making whatever kind of Blackberry game you'd like. Your logic may have to be much more complex, and you may have to include additional classes to accommodate everything, but the basic principles stay the same. Also - all the classes used here are unsigned and available without a license from Research in Motion, which means you can make, play, and distribute these games for free.

Feel free to comment if you have any questions, or even suggestions! These were my own experiences and suggestions with creating a Blackberry game, but you might have your own! Thanks for checking out this tutorial, good luck - and most importantly, have fun!

No comments yet.

Write a comment: