繁体   English   中英

Audiosystem 过早停止写入 AudioInputStream

[英]Audiosystem stops writing AudioInputStream too early

由于限制,我需要在第一次启动时下载一个 wav 文件。

我将该文件托管在服务器foo.bar/foo.wav上。

如果我在剪辑中打开与 url 和 pipe 的连接,则音频播放正常。

我这样做是使用:

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;
}

现在,当我想使用以下命令将输入流保护到 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();
    }
}

现在我的问题是,只有一部分音频,准确地说是 1s,被保存到 wav 文件中。

我已经检查过了,我在服务器上有完整的文件,我也验证过,将音频输入流传送到剪辑中并播放它会播放完整的音频。

现在我的问题:我如何将完整的 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 写入文件,或者我什至必须这样做,并且有一种更简单的方法可以将该 wav 文件下载到特定位置

好吧,我找到了解决我的问题的方法,现在我不再从服务器获取 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