Linksys
Linksys

Reputation: 1

Java UnsupportedAudioFileException when creating WAVE file from SourceDataLine

I am sorry about my English.

I am trying to create a program that records my speaker audio and saves it to a file. to create a WAVE file from my SourceDataLine I followed the steps in this question [https://stackoverflow.com/questions/9573920/how-can-i-write-the-contents-of-a-sourcedataline-to-a-file]

and made the following code

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

the output of the code is :

javax.sound.sampled.UnsupportedAudioFileException: Stream of unsupported format at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1014) at Main.main(Main.java:34)

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

Upvotes: 0

Views: 173

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

A SourceDataLine is used to sink audio data for playback. Usually a TargetDataLine, an AudioInputLine or byte arrays are used as a data source for the SourceDataLine. I'd look to get the data directly from one of those sources and avoid intermediary use of a SourceDataLine.

Upvotes: 0

Related Questions