SoundEffects.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.media.*;
- import java.io.*;
- class SoundEffects
- {
- private static SoundEffects instance;
- private Player blastSoundPlayer;
- private SoundEffects()
- {
- blastSoundPlayer = createPlayer("/blast.wav", "audio/x-wav");
- }
- static SoundEffects getInstance()
- {
- if (instance == null)
- {
- instance = new SoundEffects();
- }
- return instance;
- }
- void startBlastSound()
- {
- startPlayer(blastSoundPlayer);
- }
- void startGameOverSound()
- {
- startPlayer(createPlayer("/gameover.mid", "audio/midi"));
- }
- void startHighScoreSound()
- {
- startPlayer(createPlayer("/highscore.mid", "audio/midi"));
- }
- private void startPlayer(Player p)
- {
- if (p != null)
- {
- try
- {
- p.stop();
- p.setMediaTime(0L);
- p.start();
- }
- catch (MediaException me)
- {
- // ignore
- }
- }
- }
- private Player createPlayer(String filename, String format)
- {
- Player p = null;
- try
- {
- InputStream is = getClass().getResourceAsStream(filename);
- p = Manager.createPlayer(is, format);
- p.prefetch();
- }
- catch (IOException ioe)
- {
- // ignore
- }
- catch (MediaException me)
- {
- // ignore
- }
- return p;
- }
- }