B4J Question How to access music CD info (TOC, CD ID, Serial number)

GuyBooth

Active Member
Licensed User
Longtime User
I'm looking for a way to access the basic information on a music CD, so I can make a request to FreeDB.
I need the CD ID (8 character hex), and the Serial number, as well as the TOC.

Any thoughts on how should I go about this in B4J?
 

stevel05

Expert
Licensed User
Longtime User
There is a library here: http://java-avm.sourceforge.net/ that would need to be wrapped. At a quick glance it does not appear to be simple and contains much more than you need.

Perhaps there are smaller libraries that do only what you want. I couldn't see any with a quick search but you may find one.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
I've seen this one, and yes it's way more than I need - but I also have zero experience in creating a wrapper.
I have found a couple of libraries that are smaller and will do some of what I need.
While I am pretty fluent with B4A I am a beginner with B4J, so I'm not sure what I can and cannot do with it. There seem to be ways to tap into external programs using jShell - but not sure how much power it has.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It looks like metamusic may be the best chance, but you'll have to build it first. I can't find a compiled jar for it.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
As a first step, I am trying to run discid.exe (http://discid.sourceforge.net/) in a shell, per this thread https://www.b4x.com/android/forum/posts/474481/
So far without success
B4X:
public Sub shTest
    Dim sh As Shell
    ' discid.exe added to /objects folder
    Dim pathToExe As String = File.Combine(File.DirApp,"discid.exe")
    sh.Initialize("test", pathToExe, Null)
    sh.RunSynchronous(-1)
End Sub

public Sub test_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log(Success)
    If Success Then
        Log("Success")
        Log(StdOut)
    Else
        Log("Error: " & StdErr)
    End If

End Sub

- the code doesn't throw any errors, but the test_ProcessCompleted event is not being triggered.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
As a first step, I am trying to run discid.exe (http://discid.sourceforge.net/) in a shell, per this thread https://www.b4x.com/android/forum/posts/474481/
- the code doesn't throw any errors, but the test_ProcessCompleted event is not being triggered.
Modified the code to this:
B4X:
Private Sub Get_DiscID
    Dim sh As Shell
    Dim PathToDiscID As String = File.Combine(File.DirApp, "discid.exe")
    sh.Initialize("DiscID", PathToDiscID, Null)
Log($"PathToDiscID is ${PathToDiscID}"$)   
    sh.RunWithOutputEvents(1000)
End Sub

public Sub DiscID_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success Then
        Log("Success")
        Log(StdOut)
    Else
        Log("Error: " & StdErr)
    End If
End Sub
The event is now raised - but there is nothing in the StdOut. (I ran the discid.exe in a command window, it returned the data I expected).

I will keep trying :)
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
I will keep trying :)
This code is working to give me the CD ID, Track Start Frames, and Disk Length in Secs:
B4X:
Private Sub Get_DiscID
    Dim sh As Shell
    Dim PathToDiscID As String = File.Combine(File.DirApp, "discid.exe")
    sh.Initialize("DiscID", PathToDiscID, Null)
    sh.run(-1)
End Sub

public Sub DiscID_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    Log($"Success: ${Success}, Exit Code: ${ExitCode}, StdOut: ${StdOut}, StdErr: ${StdErr}"$)
    If Success Then
        ' DiscID is first group
        DiscID = StdOut.SubString2(0,8)
        Dim NewStdOut As String
        NewStdOut = StdOut.SubString(StdOut.IndexOf(" ")+1)
        ' Last loop is not a track,    so start at 0 instead of 1
        DiscTracks = 0
        Do While NewStdOut.Contains(" ")
            NewStdOut = NewStdOut.SubString((NewStdOut.IndexOf(" ")+1))
            DiscTracks = DiscTracks + 1
        Loop
        ' Remaining string is the length
        DiscLengthSecs = NewStdOut
        ' discid returns Discid, TrackStartFrames, Disc Length in seconds
        Log($"Disc ID is: ${DiscID}, No of Tracks: ${DiscTracks}, Length in Seconds: ${DiscLengthSecs}"$)
    Else
        Log("Error: " & StdErr)
    End If
End Sub
Edit note: The above code is not quite correct - the first group after the DiscID is the number of tracks.
I will revise this code during the next couple of days, to make it correct for future users.
 
Last edited:
Upvote 0
Top