B4J Library jAudioClip

This is a wrapper for the JavaFX AudioClip class. the Oracle documentation is here.

It is similar to the Android SoundPool in that it is intended to play short audio files with low latency, mainly for use with games and audio effects.

You can control the volume, pan, rate, looping (cyclecount, set to -1 to play indefinitly) and priority.

AudioClip
Author:
Steve Laming
Version: 1.0

  • Methods:
    • Balance(balance As double) As double
      Get / Set the default balance level for this clip.

      CycleCount(count As int) As int
      Get / Set the default cycle count.

      Initialize(URI As java.lang.String) As void
      Initialize the object.
      Requires a URI obtainable as File.GetURI(Dir,FileName))


      isPlaying As boolean
      Indicate whether this AudioClip is playing.

      Pan(pan As double) As double
      Get / Set the default pan value.

      Play
      Play the AudioClip using all the default parameters.

      Play2(volume As double)
      Play the AudioClip using all the default parameters except volume.

      Play3(volume As double, balance As double, rate As double, pan As double, priority As int)
      Play the AudioClip using the given parameters.

      Priority(priority As int) As int
      Get / Set the default playback priority.

      Rate(rate As double) As double
      Get / Set the default playback rate.

      SetCompleteListener (Module As Object, EventName As String)
      Set the callback sub for the complete listener.

      Source As String
      Get the source URL used to create this AudioClip.

      Stop
      Immediately stop all playback of this AudioClip.

      Volume(value As double) As double
      Get / Set the default volume level.

AudioClip_Static

  • Methods:
    • INDEFINATE
      When cycleCount is set to this value, the AudioClip will loop continuously until stopped.

      NewAudioClip](Source As String) As Audioclip
      Create an AudioClip loaded from the supplied source URL.

      NewAudioClip2](Folder As String,FileName As String) As Audioclip
      Create an AudioClip loaded from the supplied source Folder and FileName..

A very simple example is attached. (jAudioClipTest.zip)

Download the jAudioClipLib.zip file, unzip and copy the jar and xml to your B4j additional libraries folder.

I haven't tested this extensively so please let me know if you find any issues.

428
 

Attachments

  • jAudioClipTest.zip
    120.3 KB · Views: 726
  • jAudioClip-Project.zip
    63 KB · Views: 326
  • jAudioClipLib.zip
    6.7 KB · Views: 400
  • JAudioClip-b4xlib.b4xlib
    2.2 KB · Views: 33
Last edited:

Kevin Golding

Member
Licensed User
Longtime User
This works a treat thank you.

Is there a way to have an event fire when the clip has stopped playing please?
 

stevel05

Expert
Licensed User
Longtime User
Updated to add helper code module and Complete listener. Full B4j code is in the jAudioClip-Project.zip
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
There is an error when not setting (i.e. initializing without setting an event) :
B4X:
   Dim AC As AudioClip = AudioClip_Static.NewAudioClip2(File.DirAssets,"1.mp3")
'    AC.SetCompleteListener(Me,"AC") 'comment that line
    AC.Play

I think to solve the problem, in your code your should check that module is not Null and eventName is not "":
EDIT: Wrong, see below
B4X:
Private Sub StartListener
    If mModule <> Null And mEventName <> "" And SubExists(mModule, mEventName & "_Complete") = False Then Return
    ListenerActive = True
    Do While ListenerActive
        Sleep(1000)
        If IsPlaying = False Then ListenerActive = False
    Loop
   
    CallSubDelayed(mModule, mEventName & "_Complete")
   
End Sub
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Edit of the above message! I made a mistake, this is the correct line:
B4X:
If mModule = Null Or mEventName = "" Or SubExists(mModule, mEventName & "_Complete") = False Then Return
 

stevel05

Expert
Licensed User
Longtime User
Thanks jmon, Project & Lib updated
 
Top