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

📄 playwav_2.java

📁 《精通JAVA手机游戏与应用程序设计》随书光盘
💻 JAVA
字号:
/*
 * 播放JAR中的wav文件
 */

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

public class PlayWav_2 extends MIDlet implements CommandListener {
	private Display display;

	private Form mainForm; // Main form

	private Command exitCommand; // Command to exit the MIDlet

	private Command playCommand; // Command to play wav file

	public PlayWav_2() {
		display = Display.getDisplay(this);
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		playCommand = new Command("Play", Command.SCREEN, 2);
		
		mainForm = new Form("Play WAV file");
		mainForm.addCommand(exitCommand);
		mainForm.addCommand(playCommand);
		mainForm.setCommandListener(this);
	}

	public void startApp() {
		display.setCurrent(mainForm);
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}

	
	public void commandAction(Command c, Displayable s) {
		if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		} else if (c == playCommand)
			playwavfile();
	}

	
	public void playwavfile() {
		try {
			// 从Jar包中读取wav文件
			InputStream in = getClass().getResourceAsStream("/test-wav.wav");
			//指定文件类型
			Player p = Manager.createPlayer(in, "audio/x-wav");
			
						p.start();
		} catch (Exception e) {

			Alert alr = new Alert("错误", "不能够播放 WAV 文件!", null,
					AlertType.ERROR);
			alr.setTimeout(Alert.FOREVER);
			display.setCurrent(alr, mainForm);
		}
	}
}

⌨️ 快捷键说明

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