如果已经回答了这个特定/简单的问题,我真的很抱歉,我似乎无法解决我遇到的这个问题。
当前,我正在尝试为我的编程语言高级自学类(class)编写一个程序,该程序涉及许多.wav文件。我正在使用Eclipse Neon,并且正在使用Java。目前,我的问题是正确导入.wav文件,然后将多个文件串联在一起的正确方法。
这是利用这些文件的类的代码。在这里随意批评我的方法,我无所适从。
private ArrayList<String> paths;
public audioGen(){
paths = new ArrayList<String>();
}
public ArrayList<String> formList(int l, String type) {
Random a = new Random();
int b;
if(type.equals("A")){
for(int x=0; x<l;x++){
b = a.nextInt(14);
paths.add("/res/"+Integer.toString(b)+".wav");
}
}
return paths;
}
@SuppressWarnings("resource")
public void soundAppend(ArrayList<String> pathList) throws IOException, UnsupportedAudioFileException {
AudioInputStream clip1 = null;
for(String path1 : pathList){
if (clip1 == null){
clip1 = AudioSystem.getAudioInputStream(new File (path1));
continue;
}
AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(path1));
AudioInputStream appendFiles = new AudioInputStream(new SequenceInputStream(clip1, clip2), clip1.getFormat(), clip1.getFrameLength() + clip2.getFrameLength());
clip1 = appendFiles;
}
AudioSystem.write(clip1, AudioFileFormat.Type.WAVE, new File("src\\audio\\completed.wav"));
clip1.close();
}
public void readList(ArrayList<String> l){
for(String s : l)
System.out.println(s);
}
我设置了文件夹,以便/ res /是AUDIOWORK包下的构建路径,而源文件位于com / rade / gen /下。这是因为它们不够接近无法访问的问题,还是我需要创建audioInputStreams才能使用它们?如果是这样,我将如何从那里将它们连接起来。
非常感谢您,如果在其他地方回答了该问题,再次表示抱歉。我花了两个星期的时间来弄清楚这是怎么回事。 :/
编辑:我想指定这是我将近3年前匆忙完成的一个项目的重写,所以其中有很多我目前尚未刷新的内容-特别是
soundAppend
方法的代码。
最佳答案
问题在于获取文件的正确路径。由于您提到已经将资源文件夹添加到类路径中,因此应该可以使用:
public ArrayList<String> formList(int l, String type) {
Random a = new Random();
int b;
if(type.equals("A")){
for(int x=0; x<l;x++){
b = a.nextInt(14);
Url url = this.getClass().getResource(Integer.toString(b)+".wav");
paths.add(url.toString());
}
}
return paths;
}
关于java - 导入和连接WAV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259446/