怎么添加背景音乐

Hello-Stone 2013-06-30 04:58:25
我编辑了一个小游戏,希望给它添上背景音乐,但是我看书上都是写一个可视化界面,然后监听按钮点击去播放音乐的,我希望能够在程序一运行的时候就自动播放音乐,程序关闭的时候音乐停止播放,因此,我想把这段播放音乐的代码(最好具有可移植性)放到我编好的主类中,然后就可以了,(之前遇到了一些问题:我曾经试着同时运行两个程序一个是音乐播放文件另一个是游戏文件,然后把音乐播放文件的窗口设置成不可见的,本来以为这样能够达到想要的效果,但是当我把setVisible()设为false时,音频文件播放了几秒钟后就自动停止了)
希望大家帮帮忙~
...全文
369 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello-Stone 2013-07-01
  • 打赏
  • 举报
回复
引用 3 楼 wuyanxue 的回复:
class AePlayWave extends Thread {
	//要播放的音频文件名 只支持.wav音频
	private String filename;
	public AePlayWave(String wavfile) {
		filename = wavfile;
	}

	public void run() {

		File soundFile = new File(filename);
		//音频输入流
		AudioInputStream audioInputStream = null;
		try {
		   /****************
			*	AudioSystem 类充当取样音频系统资源的入口点
			*	此类允许查询和访问安装在系统上的混频器
			*	getAudioInputStream(File file) throws UnsupportedAudioFileException,IOException
            *	从提供的file获得音频输入流
			*	该file必须指向有效的音频文件数据
			****************/
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		} catch (Exception e1) {
			e1.printStackTrace();
			return;
		}
		//获得此音频输入流中声音数据的音频格式
		AudioFormat format = audioInputStream.getFormat();
		
		SourceDataLine auline = null;
		//根据指定信息构造数据行的信息对象,这些信息包括单个音频格式
		//此构造方法通常由应用程序用于描述所需的行
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

		try {
		   /*******************
			*  getLine(Line.Info info) throws LineUnavailableException
            *  获得与指定 Line.Info 对象中的描述匹配的行
			*  如果请求 DataLine且info是DataLine.Info的实例(至少指定一种完全限定的音频格式)
			*  则上一个数据行将用作返回的 DataLine 的默认格式
			*******************/
			auline = (SourceDataLine) AudioSystem.getLine(info);
		   /*******************
			* open(AudioFormat format) throws LineUnavailableException
			* 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作
			*******************/
			auline.open(format);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		//线程开始 播放音频
		auline.start();
		int nBytesRead = 0;
		//这是缓冲
		byte[] abData = new byte[512];

		try {
			while (nBytesRead != -1) {
			   /****************
				* read(byte[] b,int off,int len) throws IOException
				* 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中
				*****************/
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0) {
					//auline是接口实例 是从哪儿调用的write(byte[],int,int)方法?
					auline.write(abData, 0, nBytesRead);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} finally {
			//drain()是接口DataLine的方法 怎么能直接调用?
			auline.drain();
			//close()是从哪儿调用的?
			auline.close();
		}

	}
}
谢谢楼主~~很好很强大~~
折柳君 2013-07-01
  • 打赏
  • 举报
回复
//播放开战声音
		AePlayWave apw=new AePlayWave("d:\\111.wav");
		apw.start();
补充:直接在我给你提供的基础上直接调用音频文件就可以了啊!
折柳君 2013-07-01
  • 打赏
  • 举报
回复
//播放游戏音频的类
class AePlayWave extends Thread {

	private String filename;
	public AePlayWave(String wavfile) {
		filename = wavfile;

	}

	public void run() {

		File soundFile = new File(filename);

		AudioInputStream audioInputStream = null;
		try {
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		} catch (Exception e1) {
			e1.printStackTrace();
			return;
		}

		AudioFormat format = audioInputStream.getFormat();
		SourceDataLine auline = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

		try {
			auline = (SourceDataLine) AudioSystem.getLine(info);
			auline.open(format);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		auline.start();
		int nBytesRead = 0;
		//这是缓冲
		byte[] abData = new byte[512];

		try {
			while (nBytesRead != -1) {
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0)
					auline.write(abData, 0, nBytesRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} finally {
			auline.drain();
			auline.close();
		}

	}

	
}
java处理文件是以流的形式啊!工作原理:音频文件以流的形式被调入内存,然后再通过调用音频设备进行音频的播放!
摆烂办不到 2013-06-30
  • 打赏
  • 举报
回复
楼主要用的话先创建一个该类的对象 参数是音乐文件名 然后在开始游戏的时候调用start()方法 背景音乐就直接播放了
摆烂办不到 2013-06-30
  • 打赏
  • 举报
回复
class AePlayWave extends Thread {
	//要播放的音频文件名 只支持.wav音频
	private String filename;
	public AePlayWave(String wavfile) {
		filename = wavfile;
	}

	public void run() {

		File soundFile = new File(filename);
		//音频输入流
		AudioInputStream audioInputStream = null;
		try {
		   /****************
			*	AudioSystem 类充当取样音频系统资源的入口点
			*	此类允许查询和访问安装在系统上的混频器
			*	getAudioInputStream(File file) throws UnsupportedAudioFileException,IOException
            *	从提供的file获得音频输入流
			*	该file必须指向有效的音频文件数据
			****************/
			audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		} catch (Exception e1) {
			e1.printStackTrace();
			return;
		}
		//获得此音频输入流中声音数据的音频格式
		AudioFormat format = audioInputStream.getFormat();
		
		SourceDataLine auline = null;
		//根据指定信息构造数据行的信息对象,这些信息包括单个音频格式
		//此构造方法通常由应用程序用于描述所需的行
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

		try {
		   /*******************
			*  getLine(Line.Info info) throws LineUnavailableException
            *  获得与指定 Line.Info 对象中的描述匹配的行
			*  如果请求 DataLine且info是DataLine.Info的实例(至少指定一种完全限定的音频格式)
			*  则上一个数据行将用作返回的 DataLine 的默认格式
			*******************/
			auline = (SourceDataLine) AudioSystem.getLine(info);
		   /*******************
			* open(AudioFormat format) throws LineUnavailableException
			* 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作
			*******************/
			auline.open(format);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		//线程开始 播放音频
		auline.start();
		int nBytesRead = 0;
		//这是缓冲
		byte[] abData = new byte[512];

		try {
			while (nBytesRead != -1) {
			   /****************
				* read(byte[] b,int off,int len) throws IOException
				* 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中
				*****************/
				nBytesRead = audioInputStream.read(abData, 0, abData.length);
				if (nBytesRead >= 0) {
					//auline是接口实例 是从哪儿调用的write(byte[],int,int)方法?
					auline.write(abData, 0, nBytesRead);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} finally {
			//drain()是接口DataLine的方法 怎么能直接调用?
			auline.drain();
			//close()是从哪儿调用的?
			auline.close();
		}

	}
}
NightKittyT 2013-06-30
  • 打赏
  • 举报
回复
我是来学习的
chinalwb 2013-06-30
  • 打赏
  • 举报
回复
很简单吧。用JMF试试。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧