想通过java播放音乐却报错,有代码,求指正~
如题~
package text;
import *;
import javax.sound.sampled.*;
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个实例
AePlayWave apw = new AePlayWave("\\d:\\tank.mp3");
apw.start();
}
}
class AePlayWave extends Thread{
private String filename;
public AePlayWave(String wavfile){
filename = wavfile;
}
public void run(){
File soundFile = new File(filename);
System.out.println(soundFile);
AudioInputStream audioInputStream = null;
try{
audioInputStream =AudioSystem.getAudioInputStream(soundFile);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(null, null);
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, nBytesRead, nBytesRead);
if(nBytesRead >= 0){
auline.write(abData, 0, nBytesRead);
}
}
}catch (IOException e){
e.printStackTrace();
return ;
}finally{
auline.drain();
auline.close();
}
}
}