⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soundmanager.java

📁 StarShip Battle 2004 game The Developing Mobile Phone Applications With J2ME Technology course provi
💻 JAVA
字号:
// ShotSounds.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, November 2004

/*  Load and play the fired shot and explosion sounds.

    There's only one fired shot sound (laser.wav) but it's
    loaded NUM_SHOT_SOUNDS times as different players. This
    means it's possible to have several shot sounds playing
    at the same time.

    There are NUM_EXPLO_SOUNDS explosion sounds in the
    files explo0.wav to expl<NUM_EXPLO_SOUNDS-1>.wav.
    There are multiple explosion sounds for the same reason
    that we have multiple shot sounds: to have overlapping
    sound. Different explosions make the overlapping more
    interesting.

    A sound is only played when it's player is in a
    PREFETECHED state, which is one step away from playing.

    When a player finishes playing a sound, we detect it with
    a PlayerListener, and explicitly put it back to the
    PREFETECHED state.
*/

import java.io.*;
import javax.microedition.media.*;


public class SoundManager implements PlayerListener
{
  // number of fired shot and explosion sounds used
  private static final int NUM_SHOT_SOUNDS = 3;
  private static final int NUM_EXPLO_SOUNDS = 5;


  // for playing sounds (fired shots and explosions)
  private Player[] shotPlayers;
  private Player[] exploPlayers;
  private Player singleSound;
  private int currExploPlayer, currShotPlayer;


  public SoundManager()
  {
    // load fired shot sounds
    System.out.println("Loading sounds");
    shotPlayers = loadSounds("/shot.wav", NUM_SHOT_SOUNDS);
    //exploPlayers = loadSounds("/explos.wav", NUM_EXPLO_SOUNDS);
    currShotPlayer = 0;
    currExploPlayer = 0;

    loadExploSounds("explos");
    
    //singleSound = loadSounds("/3_frags.wav", 0);
    
  }  // end of ShotSounds()



  // ------------------ loading ---------------------

  private void loadExploSounds(String fn)
  // load fn + ((0 to (NUM_EXPLO_SOUNDS-1) + ".wav" sounds
  {
    exploPlayers = new Player[NUM_EXPLO_SOUNDS];
    currExploPlayer = 0;
    for (int i=0; i < NUM_EXPLO_SOUNDS; i++)
      exploPlayers[i] = loadSound(fn + i + ".wav");
  } // end of loadExploSounds()


  private Player[] loadSounds(String fn, int num)
  // load num copies of sound in fn
  {
    Player[] ps = new Player[num];
    for (int i=0; i < num; i++)
      ps[i] = loadSound(fn);
    return ps;
  }  // end of loadSounds()


  private Player loadSound(String fn)
  // load fn sound
  {
    Player p = null;
	try{
      InputStream in = getClass().getResourceAsStream(fn);
      p = Manager.createPlayer(in,"audio/x-wav");
      p.realize();
      p.prefetch();    // move player to PREFETECHED state
      p.addPlayerListener(this);
	}
    catch(Exception ex)
    { System.out.println("Could not load sound in " + fn);  }

    return p;
  } // end of loadSound()



  // --------------- playing --------------------------

  public void playExplosionSound()
  { playSound( exploPlayers[currExploPlayer] );
    currExploPlayer = (currExploPlayer+1)%NUM_EXPLO_SOUNDS;
  } // end of playExplosionSound()


  public void playShotSound()
  { playSound( shotPlayers[currShotPlayer] );
    currShotPlayer = (currShotPlayer+1)%NUM_SHOT_SOUNDS;
  } // end of playShotSound()
  
  public void playSingleSound(String fn)
  { 
  	playSound(loadSound(fn));
  } // end of playSingleSound()  

  public void playSingleSoundBGM(String fn)
  { 
  	playSound(loadSound(fn), 1);
  } // end of playSingleSoundBGM() 

  private void playSound(Player p)
  // Only start a player if it's in a PREFETECHED state.
  {
    if (p != null) {
      if (p.getState() == Player.PREFETCHED) {
	    try{
          p.setMediaTime(0);    // rewind (a safety precaution)
          p.start();
	    }
        catch(Exception ex)
        { System.out.println("Could not play sound ");  }
      }
    }
  }  // end of playSound()
  
  private void playSound(Player p, int loopCount)
  // Only start a player if it's in a PREFETECHED state.
  {
    if (p != null) {
      if (p.getState() == Player.PREFETCHED) {
	    try{
          p.setMediaTime(0);    // rewind (a safety precaution)
          p.setLoopCount(loopCount);
          p.start();
	    }
        catch(Exception ex)
        { System.out.println("Could not play sound ");  }
      }
    }
  }  // end of playSound()  



  // ----------------- finishing off --------------------


  public void playerUpdate(Player p, String event, Object eventData)
  // reset the player, ready for its next use
  {
	if (event == END_OF_MEDIA) {
      try {
        p.stop();           // back to PREFETECHED state
		p.setMediaTime(0);  // rewind to the beginning
      }
       catch(Exception ex) {}
    }
  }  // end of playerUpdate()


  public void closeSounds()
  // tidy up by closing all the players down
  {
    for(int i=0; i < NUM_SHOT_SOUNDS; i++)
      shotPlayers[i].close();
    for(int i=0; i < NUM_EXPLO_SOUNDS; i++)
      exploPlayers[i].close();
  }

}  // end of ShotSounds class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -