B4J Library jAudioClip

Discussion in 'B4J Libraries & Classes' started by stevel05, Feb 14, 2014.

  1. stevel05

    stevel05 Expert Licensed User

    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
     

    Attached Files:

    Last edited: Jul 19, 2019
    micro, rbghongade, Mashiane and 8 others like this.
  2. Kevin Golding

    Kevin Golding Member Licensed User

    This works a treat thank you.

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

    stevel05 Expert Licensed User

    Updated to add helper code module and Complete listener. Full B4j code is in the jAudioClip-Project.zip
     
    Last edited: Jul 14, 2019
    jmon, OliverA and Erel like this.
  4. jmon

    jmon Well-Known Member Licensed User

    There is an error when not setting (i.e. initializing without setting an event) :
    Code:
    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
    Code:
    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: Jul 19, 2019
  5. jmon

    jmon Well-Known Member Licensed User

    Edit of the above message! I made a mistake, this is the correct line:
    Code:
    If mModule = Null Or mEventName = "" Or SubExists(mModule, mEventName & "_Complete") = False Then Return
     
    stevel05 likes this.
  6. stevel05

    stevel05 Expert Licensed User

    Thanks jmon, Project & Lib updated
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice