java音频播放示例分享(java如何播放音频)
这是一份精简后的代码,能够支持的格式十分有限。
package com.hongyuan.test;
import java.io.file;
import java.io.ioexception;
import javax.sound.sampled.audioformat;
import javax.sound.sampled.audioinputstream;
import javax.sound.sampled.audiosystem;
import javax.sound.sampled.dataline;
import javax.sound.sampled.lineunavailableexception;
import javax.sound.sampled.sourcedataline;
import javax.sound.sampled.unsupportedaudiofileexception;
public class musictest {
public static final string music_file = "相逢一笑.wav";
public static void main(string[] args) throws lineunavailableexception,
unsupportedaudiofileexception, ioexception {
// 获取音频输入流
audioinputstream audioinputstream = audiosystem
.getaudioinputstream(new file(music_file));
// 获取音频编码对象
audioformat audioformat = audioinputstream.getformat();
// 设置数据输入
dataline.info datalineinfo = new dataline.info(sourcedataline.class,
audioformat, audiosystem.not_specified);
sourcedataline sourcedataline = (sourcedataline) audiosystem
.getline(datalineinfo);
sourcedataline.open(audioformat);
sourcedataline.start();
/*
* 从输入流中读取数据发送到混音器
*/
int count;
byte tempbuffer[] = new byte[1024];
while ((count = audioinputstream.read(tempbuffer, 0, tempbuffer.length)) != -1) {
if (count > 0) {
sourcedataline.write(tempbuffer, 0, count);
}
}
// 清空数据缓冲,并关闭输入
sourcedataline.drain();
sourcedataline.close();
}
}
上一篇: STM32F030 定时器
下一篇: css选择器优先级及权重计算