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

📄 soundeffects.java

📁 使用java 开发的手机小游戏
💻 JAVA
字号:

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

class SoundEffects {
	private static SoundEffects instance;

	private Player backgroundPlayer;

	private SoundEffects() {
		backgroundPlayer = createPlayer("/background.mid", "audio/midi");
	}

	static SoundEffects getInstance() {
		if (instance == null) {
			instance = new SoundEffects();
		}
		return instance;
	}

	void startBackgroundSound() {
		startBackPlayer(backgroundPlayer);
	}

	void stopBackgroundSound() {
		stopPlayer(backgroundPlayer);
	}

	void startGameOverSound() {
		startEffectSound(createPlayer("/gameover.mid", "audio/midi"));
	}
	void stopGameOverSound(){
		stopPlayer(createPlayer("/gameover.mid", "audio/midi"));
	}

	void startHighScoreSound() {
		startEffectSound(createPlayer("/highscore.mid", "audio/midi"));
	}
	void stopHighScoreSound(){
		stopPlayer(createPlayer("/highscore.mid", "audio/midi"));
	}

	private void startBackPlayer(Player p) {
		if (p != null) {
			try {
				p.stop();
				p.setLoopCount(-1);
				p.start();
			} catch (MediaException me) {
				// ignore
			}
		}
	}
	private void startEffectSound(Player p){
		if(p != null) {
			try {
				p.stop();
				p.setLoopCount(1);
				p.start();
			} catch (MediaException me) {
				// ignore
			}
		}
	}

	protected void stopPlayer(Player p) {

		if (p != null) {
			try {
				p.stop();
			} catch (MediaException me) {
				System.out.println("stop problem");
			}
		}
	}

	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;
	}
}

⌨️ 快捷键说明

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