Implementing the game sounds

The game sounds are implemented with the GameSoundManager class, which uses the Mobile Media API to create a Player object for each sound needed in the game, and then uses these to play the actual sound.

To implement the game sounds:

  1. Use the createToneSequencePlayer method to play MIDI files.

        private Player createToneSequencePlayer() throws IOException,
                MediaException {
            Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
            p.realize();
            ToneControl c = (ToneControl) p.getControl("ToneControl");
            c.setSequence(createSequence());
            p.prefetch();
            return p;
        }
  2. Use the createSequence method to create the tone sequence played in the game.

        private byte[] createSequence() {
            byte TEMPO = 30; // set tempo to 120 bpm, tempo is formed by multiplying the tempo modifier by 4
            byte volume = 100;
            byte d = 8; // eighth note
            byte C = ToneControl.C4;
            byte D = (byte) (C + 2);
            byte E = (byte) (C + 4);
    
            byte[] sequence = {
                ToneControl.VERSION, 1, // always 1
                ToneControl.TEMPO, TEMPO, // set the tempo
                ToneControl.SET_VOLUME, volume, // Set the new volume
                ToneControl.BLOCK_START, 0, // define block 0
                C, d, D, d, E, d, // define repeatable block of 3 eighth notes
                ToneControl.BLOCK_END, 0, // end block 0
                ToneControl.PLAY_BLOCK, 0, // play block 0
                ToneControl.SILENCE, d, E, d, D, d, C, d, // play some other
                ToneControl.PLAY_BLOCK, 0, // play block 0 again
            };
            return sequence;
        }