B4J Library jAudioRecord

This is an Audio Recording Library based on javax.sound.sampled that I have tried to make as simple to use as possible. It will record from the default device as selected in the Control Panel or Mixer (on Windows Machines).

I haven't been able to test it on Linux and Mac, but as it only uses the default devices, I hope it should be OK. I may be able to test it on Linux soon. If you run it on any system, please let me know how it runs.

The Java Audio system is a great deal more complicated than the Android equivalent, I may look at implementing more features such as input / output selection and accessing Volume and Pan controls if there is a demand for it.

My sound card doesn't support 8bit recording so I would be grateful if someone could test that. I get a lot of noise and was about to investigate when I thought I should check if the Realtek HD actually did 8bit, and apparently it doesn't so I was getting a 0 byte for every valid data byte, hence the noise.

The library:

Steve Laming
Version: 1.0

  • Methods:
    • AddLineListener(EventName As String)
      Adds a listener to this line.
      Callback to sub {EventName}_Event will be called when one of the following events has been raised:

      Available As Int
      Obtains the number of bytes of data currently available
      to the application for processing in the data line's internal buffer.

      Buffersize As Int
      Get the buffer size of the TargetDataLine

      Drains queued data from the line by continuing data I/O until the data line's internal buffer has been emptied.

      Flushes queued data from the line.

      Initialize(SampleRateHZ As Float, SampleSizeInBits As Int, ChannelConfig As Int) As Boolean

      IsActive As Boolean
      Indicates whether the line is engaging in active I/O (such as playback or capture). When an inactive line becomes active, it sends a START event to its listeners.
      Similarly, when an active line becomes inactive, it sends a STOP event.

      IsInitialized As Boolean

      IsRunning As Boolean
      Indicates whether the line is running. The default is false. An open line begins running when the first data is presented in response to an invocation of the start method,
      and continues until presentation ceases in response to a call to stop or because playback completes.

      LastException As String
      If an error occurred get the last exception

      Read(Data As Byte(), Off As Int, Len As Int) As Int
      Reads data from the TargetDataLine, it requests len bytes which are stored in data
      off = offset into array data
      Returns the actual number of bytes read

      Closes the line and

      Start capturing data

      Stop capturing data

There are no external dependencies for the Library.

The Demo is a modified version of the Android one and depends on libraries:


The threading library is necessary so we can control the processing of data as it arrives within B4j. The data capture is blocking, so if you run it on the GUI thread, the app will lock.

It captures PCM Signed linear data. No Mp3 or other formats, although access to the samples is available if you have an encoder library. The demo captures the input stream and saves it to a wav file and plays it back through MediaPlayer.

You cannot debug an application that uses the threading library, so if you are just testing and don't need a Gui, run the record subroutine in the Gui thread.

Unzip jAudioRecord and copy the xml and jar files to your addl Libraries folder.


  • jAudioRecord.zip
    4.3 KB · Views: 703
  • ARTest.zip
    2.4 KB · Views: 608
Last edited:


Active Member
Licensed User
I receive a message in the Logs
Program started.
Getting line from Audiosystem
Pos 40
Thread Rec EndedOK false Exception : wrong number of arguments
DataSize 0


Licensed User
It is not possible to run an app that contains a thread in debug mode.


Active Member
Licensed User
It is not possible to run an app that contains a thread in debug mode.

Runs as expected in Release compile....

Have a Mac OS X Lion and ran the Test Record program on it.
Works as expected...
Attach some screenshots of Mac which may interest you.
Thanks for great Library.


  • MacRecord.zip
    52.1 KB · Views: 280


Licensed User
Yes, looks good.

Thanks for the pics.

If you want to test it in debug mode, remove the thread start line and call the sub directly, it will run on the gui thread, but you won't be able to interact with any Gui you may add.


Licensed User
Hi Stevel05,

Supposedly if i have multiple audio adapters, all of them capable of doing recording.
1. is this library able to select which adapter to use ?
2. is this library able to work on different threads ?


Licensed User
I haven't looked at it for a while but as far as I remember it records the default audio input.

Threading is controlled by the B4j code, so yes.