B4J Library [B4j] jAudioTrack2

This is an new version of the jAudioTrack library (but not a direct plugin replacement) provided as a b4xlib and uses current B4x practices and classes.

It allows selecting the audio output. It also includes the Clip class which allows control over the playback position and looping.

1659862838219.png
1659862874727.png

The library no longer relies on the threading library.

There are 3 demo projects.
  • jAudioTrack-Demo
    • provides the same functionality as the original jAudioTrack library i.e. Plays a file or URL to the selected device and reads the correct audioformat from the stream.
  • jAudioTrack2-ClipDemo
    • Plays a file or URL using the Clip class, which allows control over the play position. Supported File size may be limited s it reads the whole file into memory.
  • jAudioRecAndPlay
    • Demonstrates recording and playing in one app (Requires the jAudioRecord2 library)

B4xlib
The jAudioTrack2 b4xlib contains the code and some necessary utilities. Feel free to unzip and inspect or change it as you wish.

Dependencies
There are no external dependencies.

Comments
There are as many device configurations as there are PC's. It does work with mine and I have an external sound card as well as an internal sound card, Whether java supports all devices will remain to be seen.

Only PCM samples are supported, MP3 or flac or other compressed files are not supported.

Please try it and let me know how you get on and report any issues.

Update to V0.11
  • Added NewAudioInputStreamFromBytes to jAudioTrack2_Utils to play from bytes array.
  • Added new demo app jAudioRecAndPlay (Requires jAudioRecord2 library)
Update to V0.12
  • Added WavRandomAccessFile (More information to follow)
  • Added ConvertMillisecondsToString to jAudiotrack2_Utils
Update to V0.13
  • Updated WavRandomAccessFile to access LIST data chunks within the Wav file. Useful for getting the List Info chunk if there is one. (see the demo in B4xWavRandomAccessFile for use)
Update to V 0.14 - 2023-Oct-14
  • WavRandomaccessFile
    • Added several convienience functions to read data in various formats
    • Made WavHeaderChunkMap available after initialization
    • Removed references to jfx for non-ui use
    • Added non-ui example
 

Attachments

  • jAudioTrack2-ClipDemo.zip
    4.7 KB · Views: 326
  • jAudioTrack2-Demo.zip
    4.5 KB · Views: 321
  • jAudioRecAndPlay.zip
    2.8 KB · Views: 312
  • JAudioTrack2-b4xlib.b4xlib
    9.9 KB · Views: 190
  • jaudiotrack2-nonui.zip
    1.1 KB · Views: 181
Last edited:

stevel05

Expert
Licensed User
Longtime User
Update to V0.11
  • Added NewAudioInputStreamFromBytes to jAudioTrack2_Utils to play from bytes array.
  • Added new demo app jAudioRecAndPlay (Requires jAudioRecord2 library)
 

kimstudio

Active Member
Licensed User
Longtime User
Thanks! It works on my Windows 11 and Java 1.8.

An issue has nothing to do with this lib but may impact some users using other languages is the string encoding of the device names, happens also for jAudioRecord2 demo:

Clipboard01.png


For my case I use following code to convert the strings and it works, not perfectly. This is not related to this lib but may be helpful for others facing same problem:

B4X:
    Dim strDev As String
    Dim strBytes() As Byte
    Dim L As List
    L.Initialize
    Devices.Initialize
    For Each MI As JavaObject In MixerInfos
        'Add the name to the list for the B4xCombobox
        strDev = MI.RunMethod("getName",Null)
        strBytes = strDev.GetBytes("ISO-8859-1")
        'strBytes = strDev.GetBytes("UTF-8")
        strDev = BytesToString(strBytes, 0, strBytes.Length, "GB2312")
        'strDev = BytesToString(strBytes, 0, strBytes.Length, "UTF-8")
        L.add(strDev)
        'Add the MixerInfo to the corresponding Devices List
        Devices.Add(MI)
    Next

Clipboard02.png
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Java 14 (jAudioRecAndPlay)
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
Capture RAW Initialized
Error occurred on line: 23 (jAudioRecord2_Utils)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.jaudiorecord2_utils._gettargetdataline(jaudiorecord2_utils.java:70)
at b4j.example.main._appstart(main.java:105)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
at java.desktop/javax.sound.sampled.AudioSystem.getTargetDataLine(AudioSystem.java:615)
... 31 more
 

stevel05

Expert
Licensed User
Longtime User
Caused by: java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
It appears that it can't find a soundcard in your pc that supports the standard format.

Which soundcard do you have? Or is it being used by something else?

It's the input line by the way (Naming convention is useless). Do you have a mic input?
 
Last edited:

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Maybe that's why Caused by: java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
 

stevel05

Expert
Licensed User
Longtime User
The Realtek will probably have a line in, you may need to enable it if you have something to plug in and record (if you have a line in port)
 

stevel05

Expert
Licensed User
Longtime User
Hmm OK, no audio inputs. If you have a web cam with a microphone it should find that if you plug it in.
 

stevel05

Expert
Licensed User
Longtime User
Yeah, I didn't trap the error. But it is a demo :)
 

stevel05

Expert
Licensed User
Longtime User
Update to V0.13
  • Updated WavRandomAccessFile to access LIST data chunks within the Wav file. Useful for getting the List Info chunk if there is one (see the demo in B4xWavRandomAccessFile for use).
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Update to V 0.14 - 2023-Oct-14
  • WavRandomaccessFile
    • Added several convienience functios to read data in various formats
    • Made WavHeaderChunkMap available after initialization
    • Removed references to jfx for non-ui use
 
Top