从 SourceDataLine 创建 WAVE 文件时的 Java UnsupportedAudioFileException

问题描述

我对我的英语感到抱歉。

我正在尝试创建一个程序来记录我的扬声器音频并将其保存到一个文件中。 从我的 SourceDataLine 创建 WAVE 文件我按照这个问题中的步骤 [https://stackoverflow.com/questions/9573920/how-can-i-write-the-contents-of-a-sourcedataline-to-a-file]

并制作了以下代码

mixer speaker = AudioSystem.getmixer( availablemixers [3] );
    AudioFormat  audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100,16,2,4,false); 
    DataLine.Info  lineInfo  = new DataLine.Info (SourceDataLine.class,audioFormat);      
    SourceDataLine sourceDataLine = (SourceDataLine) speaker.getLine(lineInfo);
    
    sourceDataLine.open(audioFormat);
    sourceDataLine.start();
    
    byte [] data = new byte [sourceDataLine.getBufferSize() ]; // i am not sure what i should put here
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
    
    File audioFile = new File("C:\\Users\\user\\Desktop\\RecordAudio\\record.wav");
    AudioSystem.write(AudioSystem.getAudioInputStream(byteArrayInputStream),AudioFileFormat.Type.WAVE,audioFile);
    

代码输出是:

javax.sound.sampled.UnsupportedAudioFileException:不支持格式的流 在 java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1014) 在 Main.main(Main.java:34)

 and I am not sure why can you tell me what am I doing wrong?

解决方法

A SourceDataLine 用于接收音频数据以供播放。通常使用 TargetDataLineAudioInputLine 或字节数组作为 SourceDataLine 的数据源。我希望直接从这些来源之一获取数据,并避免中间使用 SourceDataLine

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...