SoundEffects.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.media.*;
  2. import java.io.*;
  3. class SoundEffects
  4. {
  5.     private static SoundEffects instance;
  6.     private Player blastSoundPlayer;
  7.     private SoundEffects()
  8.     {
  9.         blastSoundPlayer = createPlayer("/blast.wav", "audio/x-wav");
  10.         
  11.     }
  12.     static SoundEffects getInstance()
  13.     {
  14.         if (instance == null)
  15.         {
  16.             instance = new SoundEffects();
  17.         }
  18.         return instance;
  19.     }
  20.     void startBlastSound()
  21.     {
  22.         startPlayer(blastSoundPlayer);
  23.     }
  24.     void startGameOverSound()
  25.     {
  26.         startPlayer(createPlayer("/gameover.mid", "audio/midi"));
  27.     }
  28.     
  29.     void startHighScoreSound()
  30.     {
  31.         startPlayer(createPlayer("/highscore.mid", "audio/midi"));
  32.     }
  33.     
  34.     private void startPlayer(Player p)
  35.     {
  36.         if (p != null)
  37.         {
  38.             try
  39.             {
  40.                 p.stop();
  41.                 p.setMediaTime(0L);
  42.                 p.start();
  43.             }
  44.             catch (MediaException me)
  45.             {
  46.                 // ignore
  47.             }
  48.         }
  49.     }
  50.     private Player createPlayer(String filename, String format)
  51.     {
  52.         Player p = null;
  53.         try
  54.         {
  55.             InputStream is = getClass().getResourceAsStream(filename);
  56.             p = Manager.createPlayer(is, format);
  57.             p.prefetch();
  58.         }
  59.         catch (IOException ioe)
  60.         {
  61.             // ignore
  62.         }
  63.         catch (MediaException me)
  64.         {
  65.             // ignore
  66.         }
  67.         return p;
  68.     }
  69. }