播放方法找不到串联的.wav文件(创建延迟)

时间:2018-10-04 12:53:05

标签: java multithreading javafx

此util方法有助于使用unix中的bash命令创建,静默和串联所选的.wav文件。由于某种原因,当我运行给定列表中.wav文件列表的方法时,.wav文件的串联仅在我两次运行同一方法时才会出现。因此playAudio()方法给我一个错误:

java.io.FileNotFoundException: temp/combine.wav (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:35)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:30)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
java.io.FileNotFoundException: temp/combine.wav (No such file or 
directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:35)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:30)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)

我的课是:

//Input of a string with destination of audio file to play - uses AudioStream and AudioPlayer for .wav files
public class PlayAudio {
private String audio;
private List<String> combineAudio = new ArrayList<>();
private AudioStream audioStream;

public PlayAudio(String audio) {
    this.audio = audio;
}

public PlayAudio(List<String> combineAudio) {
    this.combineAudio = combineAudio;
    audio = "temp/combine.wav";
}

public void playAudio() {
    Task task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            InputStream in = null;
            try {
                in = new FileInputStream(audio);
                audioStream = new AudioStream(in);
                AudioPlayer.player.start(audioStream); // play the audio clip with the audio player class
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    new Thread(task).start();
}

public void playCombinedAudio() {
    //Silence noise sections of each audio
    for (String audio : combineAudio) {
        String silenceAudio = "cd data/names\n" +
                "cp " + audio + ".wav ../../temp/" + audio + ".wav\n" +
                "cd ../../temp\n" +
                "ffmpeg -hide_banner -i " + audio + ".wav -af silenceremove=1:0:-30dB:1:5:-30dB " + audio + "CONCAT.wav\n";
        ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", silenceAudio);
        try {
            processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Combine audio clips together
    FileWriter writer = null;
    try {
        writer = new FileWriter("temp/combineAudio.txt");
        for (String str : combineAudio) {
            writer.write("file '" + str + "CONCAT.wav'\n");
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Concatenate from txt file
    String concatAudio = "cd temp/\n" +
            "ffmpeg -f concat -safe 0 -i combineAudio.txt -c copy combine.wav\n";
    ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", concatAudio);
    try {
        processBuilder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

    playAudio();
}

0 个答案:

没有答案