java⾳频播放⽰例分享(java如何播放⾳频)这是⼀份精简后的代码,能够⽀持的格式⼗分有限。
复制代码代码如下:
package st;
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 = Format();
// 设置数据输⼊
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 = ad(tempBuffer, 0, tempBuffer.length)) != -1) {
spring怎么读语音播放if (count > 0) {
sourceDataLine.write(tempBuffer, 0, count);
}
}
// 清空数据缓冲,并关闭输⼊
sourceDataLine.drain();
sourceDataLine.close();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。