首页 新闻 搜索 专区 学院

java音频

0
[已关闭问题] 关闭于 2017-06-06 19:27
publicclassTestController {
publicstaticvoidmain(String[] args) {

String path = "E:\\Idea\\workspace\\gisdispatch-aggregator\\gd-web\\src\\main\\webapp\\download\\测试音乐文件.wav";

VoiceRecordplayController voiceRecordplayController = newVoiceRecordplayController(path);
voiceRecordplayController.start();
}
}

publicclassVoiceRecordplayController extendsThread{

privateString filename;



publicVoiceRecordplayController(String filename){
this.filename = filename;

}

@Override
publicvoidrun(){

//定义文件对象引用filename
File sounrceFile = newFile(filename);
//定义AudioinputStream用于接收输入的音频数据
AudioInputStream audioInputStream = null;

try{
//使用AudioSystem获取音频的音频输入流
  这一步出错了
audioInputStream = AudioSystem.getAudioInputStream(sounrceFile);
} catch(UnsupportedAudioFileException e) {
e.printStackTrace();
return;
} catch(IOException e) {
e.printStackTrace();
return;
}

//用AudioFormat来获取AudioInputStream的格式
AudioFormat audioFormat = audioInputStream.getFormat();
//源数据行SoureDataLine是可以写入数据的数据行
SourceDataLine sourceDataLine = null;
//获取受数据行支持的音频格式DataLiine.info
DataLine.Info dataLineinfo = newDataLine.Info(SourceDataLine.class,audioFormat);

try{
//获取与指定info类型相匹配的行
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineinfo);
//打开具有指定格式的行,可使行获得所有所需要系统资源并变得可操作
sourceDataLine.open();
} catch(LineUnavailableException e) {
e.printStackTrace();
return;
}

//允许某一个数据行执行数据i/o
sourceDataLine.start();

//写出数据
intbytesRead = 0;
byte[] bytes = newbyte[2];

//从音频流读取指定的最大数据量的数据字节,并将其放入给定的字节数组中
try{
while(bytesRead != -1){
bytesRead = audioInputStream.read(bytes,0,bytes.length);
//通过此源数据行将数据写入混频器
if(bytesRead >= 0){
sourceDataLine.write(bytes,0,bytesRead);
}
}
} catch(IOException e) {
e.printStackTrace();
return;
} finally{
sourceDataLine.drain();
sourceDataLine.close();
}

}
}


错误提示:(各位大佛帮下忙啊)

Connected to the target VM, address: '127.0.0.1:57664', transport: 'socket'
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
    at com.uhope.gisdispatch.web.controller.VoiceRecordplayController.run(VoiceRecordplayController.java:35)
Disconnected from the target VM, address: '127.0.0.1:57664', transport: 'socket'

Process finished with exit code 0


骚年,你渴望力量吗的主页 骚年,你渴望力量吗 | 初学一级 | 园豆:45
提问于:2017-02-08 11:32
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册