java播放音频文件时出现错误
//播放声音package com.dishizhang;
import *;
import javax.sound.sampled.*;
public class Demo10_8
{
public static void main(String[] args) {
AePlayWave apw=new AePlayWave("d:\\2.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 el)
{ el.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[1024];
try{ while(nBytesRead!=-1)
{
nBytesRead=audioInputStream.read(abData,0,1024);
if(nBytesRead>=0)
auline.write(abData,0,nBytesRead);
}
}catch(Exception e){
e.printStackTrace();
return;
}finally
{ auline.drain();
auline.close();
}
}
}程序运行出现错误,咋解决。
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170)
at com.dishizhang.AePlayWave.run(Demo10_8.java:21)