【问题标题】:Java UnsupportedAudioFileException when creating WAVE file from SourceDataLine从 SourceDataLine 创建 WAVE 文件时出现 Java UnsupportedAudioFileException
【发布时间】:2021-02-14 18:18:13
【问题描述】:

对不起我的英语。

我正在尝试创建一个程序来记录我的扬声器音频并将其保存到文件中。 从我的 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, 44100 , 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?

【问题讨论】:

    标签: java audio screen-recording speaker javax.sound.sampled


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2018-05-03
      • 2017-01-25
      相关资源
      最近更新 更多