user851649
user851649

Reputation: 31

How to capture audio from microphone and save it in FLAC file format type in Java?

I need to make a simple app in java, that capture some audio from microphone, and save it in file, using FLAC. I found tutorial : http://www.developer.com/java/other/article.php/2105421/Java-Sound-Capturing-Microphone-Data-into-an-Audio-File.htm , but I need save file in flac audio, so I added jFLAC library, and I'm simply doing:

AudioSystem.write(new AudioInputStream(targetDataLine), FlacFileFormatType.FLAC, new File("junk.flac"));

but I get an exception :

java.lang.IllegalArgumentException: could not write audio file: file type not supported: FLAC
at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1346)
at pl.com.stream.snippet.concurentmap_test.AudioRecorder02$CaptureThread.run(AudioRecorder02.java:211)

Is there any example or tutorial, that shows, how to save audio file in flac format in Java?

Upvotes: 2

Views: 2525

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

From the info. page on JavaSound:

Service Provider Interface

The Java Sound API uses a Service Provider Interface to identify encoders & decoders for sound formats and sequence types. This way, adding support for a new format or type is as simple as providing a decoder and/or encoder for it, adding an SPI file to the manifest of the Jar it is in, then adding the Jar to the run-time class-path of the application. ...

Further quote from that page:

Java Sound Capabilities

The capabilities of the sampled sound API can be obtained using such methods as AudioSystem.getAudioFileTypes() ...


I think if you iterate the array returned by that method, you will find that FLAC is not listed. The solution is to add a FLAC encoder using the SPI.

Upvotes: 2

Related Questions