Wav文件arrayList。为什么这条线不起作用? Java的

时间:2013-04-14 13:25:20

标签: java arrays arraylist wav javasound

我有一个字符串arraylist,逐个浏览此列表,它应该播放wav文件。

在这一行“AudioInputStream ais = AudioSystem.getAudioInputStream(music(x));”音乐无法识别,它表示“找不到符号”。另外(music.get(x))不起作用。然后用下划线标出整条线,说明它不适用。我需要做什么?感谢。

    String trackName;

    ArrayList<String> music = new ArrayList();
    music.add("c:\\01.wav");
    music.add("c:\\02.wav");
    music.add("c:\\03.wav");
    music.add("c:\\04.wav");

    byte[] buffer = new byte[4096];
    for(int x = 0; x < music.size(); x++){


      try{

            AudioInputStream ais = AudioSystem.getAudioInputStream(music(x));
            trackName = "The Current Track is " + music.get(x);
            System.out.println(trackName);
            AudioFormat format = ais.getFormat();
            SourceDataLine line = AudioSystem.getSourceDataLine(format);
            line.open(format);
            line.start();

      while (ais.available() > 0) {

            int len = ais.read(buffer);
            line.write(buffer, 0, len);

           }

        line.drain(); //wait for the buffer to empty before closing the line
        line.close();

            } 
      catch (Exception e) {

            e.printStackTrace();

            }
    }   
}

1 个答案:

答案 0 :(得分:3)

尝试更改行:

AudioInputStream ais = AudioSystem.getAudioInputStream(music(x));

为:

AudioInputStream ais = AudioSystem.getAudioInputStream(new File(music.get(x)));

AudioSystem不会将字符串作为参数来获取音频输入流。它需要InputStreamURLFile,我在上面演示过。