B4J Question MP3 Player in B4J non-Ui Application

Discussion in 'B4J Questions' started by Pedro Caldeira, Apr 16, 2018.

  1. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Hello all,
    I have to code a simple application to play a mP3 file in linux command line.
    like : PlayMp3 [File to play]

    It works in Ui mode with Jfx, but not in non-Ui applications. Is there a way to solve this ?
     
    Last edited: Apr 16, 2018
  2. stevel05

    stevel05 Expert Licensed User

    What are you using to play the MP3's ?
     
  3. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    jFX library

    Code:
    #Region Project Attributes
        
    #CommandLineArgs: confirmaconta.mp3
        
    #MergeLibraries: True
    #End Region

    Sub Process_Globals
        
    Dim Mp3 As MediaPlayer
    End Sub

    Sub AppStart (Args() As String)
        
    If Args.Length <> 1 Then
            
    Log("pMP3 [File To Play]")
            ExitApplication2(
    1)
        
    End If
     
        playMP3(Args(
    0))
        StartMessageLoop
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
        
    Return True
    End Sub

    Sub playMP3(File2Play As String)
        
    If File.Exists(File.DirApp,File2Play) = False Then
            
    Log("53")
            ExitApplication2(
    53)
        
    End If
     
        Mp3.Initialize(
    ""File.GetUri(File.DirApp, File2Play))
        Mp3.Play
        
    Log("0")
        ExitApplication2(
    0)
    End Sub
     
  4. stevel05

    stevel05 Expert Licensed User

    You will need to use a different method to play the MP3's in a non gui environment. The Media player will have a dependency on the Javafx framework which is not available for a non Gui application.

    As an experiment I found a player here: https://code.google.com/archive/p/mp3transform/downloads.

    If you download the zip file there is a jar file in there. You can copy that to your additional libs directory then run this code :

    Code:
    'Non-UI application (console / server application)
    #Region Project Attributes
        
    #CommandLineArgs:
        
    #MergeLibraries: True
    #End Region
    #AdditionalJar: mp3transform

    Sub Process_Globals
     
    End Sub
    'https://code.google.com/archive/p/mp3transform/downloads
    Sub AppStart (Args() As String)
     
        
    File.Copy(File.DirAssets,"01-Billet Doux.mp3",File.DirApp,"01-Billet Doux.mp3")
        
    Dim Player As Reflector
     
        Player.Target = Player.CreateObject(
    "org.mp3transform.awt.PlayerNoCover")
        
    Dim jFile As JavaObject
        jFile.InitializeNewInstance(
    "java.io.File",Array(File.DirApp,"yoursongfile.mp3"))
        
        Player.RunMethod4(
    "run",Array(Array As String("")),Array As String("[Ljava.lang.String;"))
        
    Dim FrameR As Reflector
        FrameR.Target = Player.GetField(
    "frame")
         FrameR.RunMethod2(
    "setVisible",False,"java.lang.boolean")
        Player.RunMethod4(
    "play",Array(jFile),Array As String("java.io.File"))
        StartMessageLoop
     
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
        
    Return True
    End Sub
    If you want to use this You will have to read the source for documentation.

    Depends on the Reflection library.

    There is a gui, but it is immediately dismissed.

    Anyway just a test.

    Looking at the internet, there are quite a few other options. You will need something that decodes mp3's.
     
    Erel and Pedro Caldeira like this.
  5. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Thanks. I will sure give it a try.
     
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