这串代码在myeclipse上面写完并没有显示错误,但是运行的时候就有错误了
package test;
import java.io.*;
import javax.sound.sampled.*;
public class TestVoice {
public static void main(String []args){
PlaySounds  ps=new PlaySounds ("E:\\KuGou\\汤旭 - 岛歌.mp3");
ps.start();
}
}
//播放声音的类
 class PlaySounds extends Thread {
private String filename;
public PlaySounds(String wavfile) {filename = System.getProperty("user.dir")+wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
 audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
 e1.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, abData.length);
  if (nBytesRead >= 0)
   auline.write(abData, 0, nBytesRead);
 }
} catch (IOException e) {
 e.printStackTrace();
 return;
} finally {
 auline.drain();
 auline.close();
}

}显示的错误提示是:
java.io.FileNotFoundException: D:\myeclipse program\testE:\KuGou\汤旭 - 岛歌.mp3 (文件名、目录名或卷标语法不正确。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(WaveFloatFileReader.java:164)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1179)
at test.PlaySounds.run(TestVoice.java:21)

解决方案 »

  1.   

    错误不是很明显吗:
    java.io.FileNotFoundException: D:\myeclipse program\testE:\KuGou\汤旭 - 岛歌.mp3 (文件名、目录名或卷标语法不正确。)
    文件路径不正确,又是D盘又是E盘的路径。
      

  2.   

    public PlaySounds(String wavfile) {
      filename = System.getProperty("user.dir")+wavfile;
    }
    这一段代码错了。直接写filename=wavefile;
    你现在这样相当于默认路径加上传进来的参数生成新路径