简体   繁体   English

AudioSystem.write(input, AudioFileFormat.Type.WAVE, file) 产生损坏的 WAVE 文件。 RIFF 长度无效

[英]AudioSystem.write(input, AudioFileFormat.Type.WAVE, file) produces a corrupt WAVE file. Invalid RIFF length

This code is producing WAV files that don't work in many apps.此代码生成的 WAV 文件在许多应用程序中都不起作用。

When I check in a RIFFVIEWER app it complains about invalid RIFF length.当我签入 RIFFVIEWER 应用程序时,它会抱怨无效的 RIFF 长度。 BWFMetaEdit claims the file is truncated. BWFMetaEdit 声称该文件已被截断。 Some tolerant apps like Audacity will play them.一些宽容的应用程序,如 Audacity 会播放它们。

Am I doing something wrong here or is java audio buggy?我在这里做错了什么还是java音频有问题?

// The essence data is PCM formatted, so convert it to a WAVE file
File extractPCM(WAVEPCMDescriptor descriptor, EssenceData data, String name) {
    try {
        Stream stream = data.getEssenceStream();
        URI uri = stream.getStreamURI();
        int hashCode = uri.hashCode();

        File file = new File(mediaDir, 
                name + "_" +
                String.format("%08X", hashCode)
                + ".wav"
                );

        if (file.exists()) {
            return file;
        }

        mediaDir.mkdir(); // Ensure exists

        log("Copying essence data stream"); 

        stream.setPosition(0);
        ByteBuffer buff = stream.read((int)stream.getLength());
        stream.close();
        buff.flip();

        AudioFormat format = new AudioFormat(
                Encoding.PCM_SIGNED,
                (float)descriptor.getSampleRate().doubleValue(),
                (int)descriptor.getQuantizationBits(),
                (int)descriptor.getChannelCount(),
                (int)descriptor.getBlockAlign(),
                (float)descriptor.getAverageBytesPerSecond(),
                false
                );
        AudioInputStream input = new AudioInputStream(
                new ByteArrayInputStream(buff.array()), format, buff.capacity());           
        AudioSystem.write(input,
                   AudioFileFormat.Type.WAVE, file);        
        log("Extracted file " + file);
        return file;
    } catch (EndOfDataException | IllegalArgumentException | IOException e1) {
        log(e1);
        return null;
    }
}

It looks like I was supplying the length as bytes instead of sample frames.看起来我提供的长度是字节而不是样本帧。

When I divide the buffer size by the frame size, non tolerant apps stop complaining.当我将缓冲区大小除以帧大小时,非容忍应用程序停止抱怨。

AudioInputStream input = new AudioInputStream(
        new ByteArrayInputStream(buff.array()), format, buff.capacity() / format.getFrameSize());

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

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