Using the Sound Manager

Now that we've made the sound manager, let's see it in action by looking at a simple application that implements it (note also that we will be using it again later when we create the game framework in Chapter 12). Let's first look at the complete code listing for the application and the expected output (although sadly this book doesn't have built-in speakers).

Code Listing 11-8: Sound manager example application

start example
import java.awt.*; import java.awt.event.*; import javax.swing.*;   public class SoundManagerExample extends JFrame implements     ActionListener {     public SoundManagerExample()     {         super("Sound Manager Example");         setDefaultCloseOperation(EXIT_ON_CLOSE);         getContentPane().setLayout(null);         setResizable(false);         setBounds(0, 0, 430, 75);                  // Initialize the Sound Manager...         soundManager = new SoundManager();                  soundId = new int[4];                  soundId[DEEPBASS_SOUND] = soundManager.addSound             ("DeepBass.wav");         System.out.println("DEEPBASS_SOUND is now defined as             "+soundId[DEEPBASS_SOUND]);                  soundId[DISCO_SOUND] = soundManager.addSound("Disco.wav");         System.out.println("DISCO_SOUND is now defined as             "+soundId[DISCO_SOUND]);                  soundId[SLOWDRUM_SOUND] = soundManager             .addSound("SlowDrum.wav");         System.out.println("SLOWDRUM_SOUND is now defined as             "+soundId[SLOWDRUM_SOUND]);                  soundId[BOOM_SOUND] = soundManager.addSound("Boom.wav");         System.out.println("BOOM_SOUND is now defined as             "+soundId[BOOM_SOUND]);                  toggleSoundButton = new JButton[soundId.length];                      // Setup the GUI...         for(int i=0; i<soundId.length; i++)         {             toggleSoundButton[i] = new JButton("Sound "+i+" Off");             toggleSoundButton[i].setBounds(5+(i*105), 5, 100, 40);             toggleSoundButton[i].setBackground(Color.red);             toggleSoundButton[i].addActionListener(this);             getContentPane().add(toggleSoundButton[i]);         }                  setVisible(true);     }          public void actionPerformed(ActionEvent e)     {         for(int i=0; i<soundId.length; i++)         {                 if(e.getSource() == toggleSoundButton[i])             {                 if(soundManager.isSoundPlaying(soundId[i]))                 {                     soundManager.stop(soundId[i]);                     toggleSoundButton[i].setText("Sound "+i+" Off");                     toggleSoundButton[i].setBackground(Color.red);                 }                 else                 {                     soundManager.play(soundId[i], true,                         SoundManager.AUTO_ASSIGN_CHANNEL);                     toggleSoundButton[i].setText("Sound "+i+" On");                     toggleSoundButton[i].setBackground(Color.green);                 }                                  return;             }         }     }                  public static void main(String args[])     {         SoundManagerExample soundManagerExample = new             SoundManagerExample();     }          public static SoundManager soundManager;          // GUI     public JButton[] toggleSoundButton;          public int[] soundId;          // Sound Definitions...     public static final int DEEPBASS_SOUND    = 0;     public static final int DISCO_SOUND       = 1;     public static final int SLOWDRUM_SOUND    = 2;     public static final int BOOM_SOUND        = 3;     }
end example

When we run this application, it should look like the following figure.

Note first though that you'll need to copy the following sound files off the companion CD into your code directory:

  • DeepBass.wav

  • Disco.wav

  • SlowDrum.wav

  • Boom.wav

click to expand
Figure 11-5: Sound manager example

Let's have a look at the relevant parts of the application. First create an instance of the sound manager by calling its constructor in the usual way. This can be seen here:

soundManager = new SoundManager();

Next, allocate an array of four integers to hold the IDs of the sounds that we are about to load. This can be seen here:

soundId = new int[4];

Then load four sounds into the sound manager by calling the addSound method, and store the ID of each sound in the appropriate position in the soundId array. The code to load one of the sounds can be seen here:

soundId[DEEPBASS_SOUND] = soundManager.addSound("DeepBass.wav");         System.out.println("DEEPBASS_SOUND is now defined as             "+soundId[DEEPBASS_SOUND]);

Notice also how we have created the following definitions to allow more readable access to the array of sound IDs.

public static final int DEEPBASS_SOUND        = 0; public static final int DISCO_SOUND           = 1; public static final int SLOWDRUM_SOUND        = 2; public static final int BOOM_SOUND            = 3;    

Hence we can get the ID for the DEEPBASE_SOUND by accessing element 0 of the soundId array.

After we have loaded the sounds, we check if the associated sound is playing on the relevant button click by calling the isSoundPlaying method and passing in the ID of the sound. If it is playing, we stop it, and if it isn't, we play it.

To stop the sound, we simply invoke the stop method of the soundManager object, passing in the relevant sound ID, which can be seen in the following line of code:

soundManager.stop(soundId[i]);

Again, to play the sound, just call the play method of the sound manager, passing in the sound ID and specifying whether it should be looped or not. In this example, we use the auto channel allocation feature by specifying SoundManager.AUTO_ASSIGN_CHANNEL as the third parameter instead of a specific channel number. This can be seen in this final line of code:

soundManager.play(soundId[i], true,     SoundManager.AUTO_ASSIGN_CHANNEL);



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net