问题描述
我对我的英语感到抱歉。
我正在尝试创建一个程序来记录我的扬声器音频并将其保存到一个文件中。 从我的 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
用于接收音频数据以供播放。通常使用 TargetDataLine
、AudioInputLine
或字节数组作为 SourceDataLine
的数据源。我希望直接从这些来源之一获取数据,并避免中间使用 SourceDataLine
。