这是我的问题,有人给了我一个函数,如果我理解得很好,它将一些声音样本放入一个数组列表中。
我想用这个音轨创建一个 .wav 文件,但我真的不知道该怎么做。
这是代码,因为也许我根本不明白......
public class Track {
private ArrayList<Sample> sounds;
private AudioFormat audioFormat;
TargetDataLine targetDataLine;
public Track()
{
this.sounds = new ArrayList <Sample>();
}
/*** Sort the sample on the track by ascending start time ***/
public void sortTrack() {
Collections.sort(sounds);
}
/**
* Add a sample to the track.
* @param fic location to the audio file.
* @param sT set the start time in ms of the sound on the track.
*/
public void addSound(String fic, long sT) {
sounds.add(new Sample(fic, sT));
}
/**
* Delete a sample to the track.
* @param fic location to the audio file.
* @param sT set the start time in ms of the sound on the track.
*/
public void deleteSound(String fic, long sT) {
int i;
for ( i = 0; i < sounds.size() &&(
sounds.get(i).getAudioFile().getName() == fic &&
sounds.get(i).getStartTime() == sT); ++i) {}
if (i < sounds.size()) sounds.remove(i);
}
这是示例,在上面的代码中导入。
public Sample (String fileLocation, long sT) {
try{
audioFile = new File(fileLocation);
istream = AudioSystem.getAudioInputStream(audioFile);
format = istream.getFormat();
startTime = sT;
timeLenght = (audioFile.length() / (format.getFrameSize() * format.getFrameRate() )) * 1000;
}
catch (UnsupportedAudioFileException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
最佳答案
在我看来,这不是一个好的 Java 源代码,因为有很多无用的操作可以使用一些特定的 Java 工具来自动化。
显然,您有一个庞大的类,它代表特定专辑的独特轨道;当然,这张专辑被分割为不同的样本。以下是方法的详细描述以及一些提示,以改进您的代码:
这样,在每次插入操作中,您将始终根据您定义的标准对数据进行排序(根据您发布的代码,每个样本都使用其开始时间进行排序);
在这种情况下,您需要修复 Sample 类中的 equals() 方法,以便定义两个 Sample 何时相等。
关于java - 将带有音频的数组列表转换为 .wav 文件(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24282109/