简体   繁体   English

Audiosystem 过早停止写入 AudioInputStream

[英]Audiosystem stops writing AudioInputStream too early

Due to restrictions, i need to download a wav file on the first startup.由于限制,我需要在第一次启动时下载一个 wav 文件。

I have that file hosted on server foo.bar/foo.wav .我将该文件托管在服务器foo.bar/foo.wav上。

If i open a Connection to that url and pipe that into a clip, the audio plays fine.如果我在剪辑中打开与 url 和 pipe 的连接,则音频播放正常。

Im doing this using:我这样做是使用:

public static AudioInputStream downloadSound(String link) {
    URL url;
    try {
        url = new URL(link);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        InputStream is = urlConn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        AudioInputStream soundInput = AudioSystem.getAudioInputStream(bis);
        // Starting the clip here also works perfectly
        return soundInput;
    } catch (IOException | UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

Now, when i want to safe the inputstream to a wav file using:现在,当我想使用以下命令将输入流保护到 wav 文件时:

public void saveSound(AudioInputStream stream, String name) {
    this.createFolderIfNotExist("/sound");
    File soundfile = new File(mainPath + "/sound/" + name); 
    Clip clip;
    try {
        clip = AudioSystem.getClip();
        clip.open(stream);
        clip.start();
        // The Clip starts normally here. ( this block is just for testing purposes)
    } catch (LineUnavailableException | IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        AudioSystem.write(stream, AudioFileFormat.Type.WAVE, soundfile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Now my Problem is, that only a part of the audio, to be precise 1s, gets saved into the wav file.现在我的问题是,只有一部分音频,准确地说是 1s,被保存到 wav 文件中。

I've already checked and i have the complete file on the server, i also have verified, piping the audioinputstream into a Clip and playing it plays the complete audio.我已经检查过了,我在服务器上有完整的文件,我也验证过,将音频输入流传送到剪辑中并播放它会播放完整的音频。

Now my question: How do i write the complete stream to a file or do i even have to do it this way and this there an simpler way to download that wav file to a specific location现在我的问题:我如何将完整的 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 写入文件,或者我什至必须这样做,并且有一种更简单的方法可以将该 wav 文件下载到特定位置

Well, i've found a workaround for my problem, now instead of getting the audioInputStream from the server im just handling the stream as a normal stream and write it using java io's Files.好吧,我找到了解决我的问题的方法,现在我不再从服务器获取 audioInputStream,而是将 stream 作为普通 stream 处理,并使用 Z93F725A07423FE1C81F448B33D 文件编写它。

public static void downloadSoundandSave(String url, String name) {
   Filesystem filesystem = new Filesystem();
    try (InputStream in = URI.create(url).toURL().openStream()) {
        filesystem.createFolderIfNotExist("/sound");
        Files.copy(in, Paths.get(filesystem.getMainPath() + "/sound/" + name));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM