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

📄 audio.java

📁 J2ME 3D 第一人称射击迷宫类手机游戏源码。
💻 JAVA
字号:
package myGame.main;

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 * <p>
 * Audio manager. Contains one player for each sound.
 * </p>
 * <p>
 * Implementation note: the format of the sound is hardcoded in method
 * <code>playSound</code>.
 * </p>
 * 
 * @author YuBingxing
 */
public class Audio {
	public static Player player = null;

	/** Jazzy background music */
	public static final int MUSIC = 0;

	/** Connection failed sound */
	public static final int CONN_FAIL = 1;

	/** Winner sound */
	public static final int WINNER = 2;

	public static final int BGMUSIC1 = 3;

	public static final int BGMUSIC2 = 4;

	public static final int BGMUSIC3 = 5;

	/** The media players */
	protected static Player[] m_sounds = new Player[8];

	/** Current playing player index */
	protected static int m_currentPlayer = -1;

	protected static final String TYPE_AMR = "audio/amr";

	protected static final String TYPE_MPEG = "audio/mpeg";

	protected static final String TYPE_MIDI = "audio/midi";

	protected static final String TYPE_WAV = "audio/x-wav";

	/**
	 * Starts playing specified sound, one of <code>Audio.MUSIC</code>,
	 * <code>Audio.DICES_LONG</code>, <code>Audio.DICES_SHORT</code>,
	 * <code>Audio.PIECE</code>. If there is another sound playing, it is
	 * stopped.
	 * 
	 * @param snd
	 *            The id of the sound to play.
	 */
	public synchronized static void playSound(int snd) {
		// Start sound
		if (!RmsFacade.getBoolean(MyGame.AUDIO_OFF)) {
			// No player, create one
			if (m_sounds[snd] == null) {
				createSound(snd);
			}
			// Start player
			if (player != null) {
				try {
					player.stop();
					player = null;
				} catch (MediaException e) {
					e.printStackTrace();
				}
			}
			player = m_sounds[snd];
			if (player != null) {
				try {
					player.start();
					m_currentPlayer = snd;
				} catch (MediaException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Stops specified sound if it is playing.
	 * 
	 * @param snd
	 *            The id of the sound to stop.
	 */
	public synchronized static void stopSound(int snd) {
		if (m_sounds[snd] != null) {
			try {
				m_sounds[snd].stop();
			} catch (MediaException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Stops all sounds and cleans up resources.
	 */
	public static void shutdown() {
		for (int i = 0; i < m_sounds.length; i++) {
			stopSound(i);
			if (m_sounds[i] != null) {
				m_sounds[i].deallocate();
			}
		}
	}

	/**
	 * Creates a player for specified sound and popuplates the Player array.
	 * 
	 * @param snd
	 *            The sound to create
	 */
	protected static void createSound(int snd) {
		try {
			int rsc = 0;
			String type = TYPE_AMR;
			switch (snd) {
			case MUSIC:
				type = TYPE_MIDI;
				rsc = Resources.SND_BACKGOUND;
				break;
			case CONN_FAIL:
				type = TYPE_MIDI;
				rsc = Resources.SND_CONNFAIL;
				break;
			case WINNER:
				type = TYPE_MIDI;
				rsc = Resources.SND_WINNER;
				break;
			case BGMUSIC1:
				type = TYPE_MIDI;
				rsc = Resources.SND_BGMUSIC1;
				break;
			case BGMUSIC2:
				type = TYPE_AMR;
				rsc = Resources.SND_BGMUSIC2;
				break;
			case BGMUSIC3:
				type = TYPE_AMR;
				rsc = Resources.SND_BGMUSIC3;
				break;
			}
			m_sounds[snd] = Manager.createPlayer(Resources.getResource(rsc),
					type);
			if (snd == MUSIC) {
				m_sounds[snd].setLoopCount(-1); // loop intro tune for ever and
				// ever
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MediaException e) {
			e.printStackTrace();
		}
	}

	/** Prevent instantiation */
	private Audio() {
	}
}

⌨️ 快捷键说明

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