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

📄 audio.java

📁 播放mid音乐的demo,架构写的很好 大家看看吧 2008.05.22写的
💻 JAVA
字号:


import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
/**
 * @use audio class
 * @author Tony
 * @time 2008-05-22
 */
public class Audio {
	private Player player;

	private String filename;

	private String format;

	public Audio(String filename, String format, boolean isLoad) 
	{
		this.format = format;
		this.filename = filename;

		if (isLoad) {
			loadResource(); 
		}
	}

	public Audio(String filename, String format) 
	{
		this.format = format;
		this.filename = filename;
		loadResource();
        }

	public void loadResource() {
		try {
			InputStream is = getClass().getResourceAsStream("/" + filename);
			player = Manager.createPlayer(is, format); 
                        setLoop();
		} catch (IOException e) {
			System.out.println("Can't load" + filename);
			System.out.println(e);
		} catch (MediaException e) {
			System.out.println("Can't create audio");
			System.out.println(e);
		}
	}

	public void setLoop() {
		if (player != null) {
			player.setLoopCount(-1); //无限循环
		}
	}

	public void setVolume(int lager) {
		if (player != null) {
			((VolumeControl) player.getControl("VolumeControl"))
					.setLevel(lager);
		}
	}

	public void stop() {
		if (player != null) {
			try {
                                setVolume(0);
				player.stop();
			} catch (MediaException e) {
				System.out.println("Can't stop audio");
				System.out.println(e);
			}
		}
	}

	public void play() {
		if (player != null) {
			try {
				player.realize();
				player.prefetch();
                                setVolume(100);
				player.start();
			} catch (MediaException e) {
				System.out.println("Can't play audio");
				System.out.println(e);
			}
		}
	}

	public void replay() {
		close();
		System.gc();
		loadResource();
		play();
	}

	public void close() {
		if (player != null) {
			player.close();
			player = null;
		}
	}
}

⌨️ 快捷键说明

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