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.

package com.synthdreams.GalacticBlast;

import net.rim.device.api.ui.component.Dialog;
import java.lang.Class;
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)
            // 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 =, "audio/midi");
            // Ready the data and start playing it.  To loop indefinitely, we set loopcount
            // to -1.

        catch (Exception e)
            Dialog.alert("Error playing music");
    // Stop playing music
    void stopMusic()
            // Tell player to stop playing
        catch (Exception e)
            Dialog.alert("Error stopping music");
        // Then release the data and close out the player
    // 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};
            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)

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!

22 Responses to Creating a Blackberry Game – Part 6

  1. Merle says:

    Thank you sir. Being unfamiliar with both Java and mobile programming this was a godsend. Like you said, every other example I saw was for generic Java and not the blackberry and thus wouldn’t compile and run correctly. I was able to copy your source and run it on my blackberry without issue and your instructions/comments gave me a good understanding of what needs to be done with these apps.

  2. dave says:

    This is an outstanding tutorial. Thanks, Toni. I am working on a graphical app that I wanted to port to Blackberry and this is exactly what I needed. I’ll send you a link and give you a credit in my app when it’s finished. Cheers!

  3. korsuas says:

    any place we can download the code from?

  4. Keith says:

    Thank you very much for taking the time to write this tutorial. I found it very useful and informative.

    Cheers to you.

  5. Chak says:

    Thank you for this! You’ve helped me immensely!

  6. Tony says:


    Fantastic tutorial!!! Is this expected to work on BB 7290?
    Looking for .mid, *.jpg & .png files??

    — Thanks

  7. Henry Salem says:

    Great Tutorial. Is it possibl to post the game files such as the graphics and sound. I want to try to test it on my blackberry.

    Thank you

  8. Toni says:

    Thanks for the kudos, I’m glad people enjoyed the tutorial! I post links to where the music and some graphics can be found on part 3

  9. Kaneda says:

    Nice tutorial.

    Great Job!!

  10. Darwin says:

    I’m getting an error with the music. I’ve provided my own midi but I’m not sure where to put it so that it gets compiled into the COD file. Commenting out the music calls I’m getting a null pointer exception. Loading screen works fine but I think I’m having issues with the resources loading correctly. I’ve tried dumping them in every possible path with no luck. Any suggestions? Using JDE 4.3 and the latest Eclipse plugin so it should work out just fine.

    Any help would be tremendous. Thanks!

  11. Toni says:

    Hi Darwin –

    I know using the Blackberry JDE I just dropped resources (including the midi file) into the root of the project, and then the getBitmapResource/getResourceAsStream methods were able to find them (I had used a leading slash for getResourceAsStream, not sure if it’s necessary but make sure you have it if you haven’t tried that).

    I haven’t worked with Eclipse using Java before, you may have to set a property of the resource to be included with the package if such a property exists (I know you have to do this same type of thing with Visual Studio and ASP.NET applications).

    Let us know if you find a solution for this when working with Eclipse.

  12. Darwin says:

    Figured it out, it was actually kind of obvious just a little different than what I’m used to. Had to throw the resource files (images/music) in a folder (just made one on c:\ for simplicity) and then right click on your package->import->general->file system. Once I got to that point I just navigated to the folder that I had my resources in, checked them all off and hit import. I originally imported them to the project root but that didn’t work, once I moved them over a couple levels to my package ‘folder’ they compiled into the COD no prob. There may be a quicker way to do this but after messing around for a while this worked for me, the “/” for the music was in fact needed as well.

    Thanks for the response and the best tutorial I’ve found so far on this subject! I still have a few kinks to work out where I went and modified the code a bit, but it seems to be a success other than that.

  13. Darwin says:

    *edit to post above – the resources go in the ‘src’ folder/filter not the package one, late here and the way it’s laid out fooled me ๐Ÿ˜›

  14. hr says:

    hey toni, first of all thanks for the great tutorial! i copied ur code to make sure i could build the game n then transfer it to my blackberry, though i encountered a few problems:

    -when hero gets hit he goes to the upper-left hand corner
    -life meter doesnt decrease when hero gets hit
    -neither enemies nor their beams are visible, but are there because i get hit

    i dont think it matters but im on eclipse and a pearl 8130. any help will be greatly appreciated! thanks!

  15. Toni says:

    Hey HR –

    Those are some strange problems, it sounds like some arguments might be getting switched around or something to that effect, or that the hero object isn’t the first one in the vector, which would happen if refresher was somehow being called before the hero object was instantiated. Are you sure the code is entered exactly as is? I would look around those locations to make sure.

    The only difference I can think of with the Pearl that would somehow affect the game is the difference in screen size, but I can’t imagine it causing those kind of issues. Let me know if you find anything with the hero not being loaded first.

  16. Timon says:

    Hi Toni,

    Thanks for this super tutorial. I have been programming Java for a while, but have just started with the RIM API. This tutorial helped familiarize me not only with using basic screens, but with more advanced graphics, sound, and multi-threading. I may have to try my hand at creating a game, just for fun. Thank you again for your time, effort, and willingness to share. Your work is greatly appreciated!

  17. Toni says:

    Thanks for the kudos Timon! Glad I could help a bit.

  18. achmed says:

    THanKS a lot bro…!

    btw, it would be nice to include the resource files such as png’s and other audio.

    an awesome tutorial….:D

  19. Kareemullah says:

    Thank You very much Sir….,

  20. Eduardo says:

    Toni, you’ve done a bunch of us a great service with this tutorial. I’ve got the demo running, made a few small tweaks, and now expect to study the code in detail. I’ve never programmed in Java before, and am generally rusty, anyway. Your code is well commented and well organized, and I know I will learn a great deal from the study. My immediate goal has been to make a better scrolling helicopter game for the BB, and now, I know that I can do it.

    I have done some games similar to this design in VB 6. In that system, we had to blit masks in place with the sprites. I can’t remember the details. But, it’s nice to see that masks will not be needed.

  21. Rama Srinivas says:


    I need all images that u have used in this project. Plz replay fast…

Leave a Reply

Your email address will not be published.