码迷,mamicode.com
首页 > 其他好文 > 详细

学习笔记三十二:音频流

时间:2015-01-11 09:39:03      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

奋斗之心人皆有之。——李叔同


本讲内容:播放音频

import java.io.*;
import javax.sound.sampled.*;

public class Text{
	public static void main(String[] args) {
		//播放开战声音
		AePlayWave apw=new AePlayWave("e:/tank.wav");
		apw.start();
	}
}

//播放声音的类
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();
		}
	}
}


本讲就到这里,Take your time and enjoy it


学习笔记三十二:音频流

标签:

原文地址:http://blog.csdn.net/liguojin1230/article/details/42592601

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!