Android Question Exoplayer update to support MP3 tags

wimpie3

Well-Known Member
Licensed User
Longtime User
Would it be possible to update the Exoplayer for B4A? In the mean while, the original library supports reading MP3 tags, which is interesting for displaying info (like the title of the current song playing) on radio streams. I desperately need this for one of my applications and I cannot find a library that does this (the AAC decoder that is often mentioned, does not work with the latest API).

See https://medium.com/google-exoplayer/exoplayer-2-1-whats-new-2832c09fedab
ExoPlayer 2.1 - What’s new
ID3 metadata and album art
ExoPlayer now parses ID3 metadata from the headers of MP3 and MP4 files.


B4X:
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
for (int i = 0; i < trackGroups.length; i++) {
TrackGroup trackGroup = trackGroups.get(i);
for (int j = 0; j < trackGroup.length; j++) {
Metadata trackMetadata = trackGroup.getFormat(j).metadata;
if (trackMetadata != null) {
// We found metadata. Do something with it here!
}
}
}
}
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Moved to the questions forum.
It is quite simple to implement this code with JavaObject:
B4X:
Sub Player_TrackChanged
   Dim jo As JavaObject = player1
   Dim TrackGroups As JavaObject = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
   For i = 0 To TrackGroups.GetField("length") - 1
       Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
       For j = 0 To TrackGroup.GetField("length") - 1
           Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
           Log(Format)
           Dim Metadata As JavaObject = Format.GetField("metadata")
           If Metadata.IsInitialized Then
               Log("Metadata: " & Metadata)
           End If
       Next
   Next
End Sub
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
if you want to get the actual metadata related to the songs/videos, you need to dive deeper into the layers available since
B4X:
Log("Metadata: " & Metadata)
will not show any useful information.

Below I am posting some quick code to get artist, album, and title of an mp3-song. It is based on Erel's code snippet above plus I added some inline java code to get the missing information. Useful information about tags can be found here:
http://id3.org/id3v2.4.0-frames

upload_2019-5-7_18-13-3.png




B4X:
Sub Player_TrackChanged
    Dim jo As JavaObject = player1
    Dim TrackGroups As JavaObject = jo.GetFieldJO("player").RunMethod("getCurrentTrackGroups", Null)
    For i = 0 To TrackGroups.GetField("length") - 1
        Dim TrackGroup As JavaObject = TrackGroups.RunMethod("get", Array(i))
        For j = 0 To TrackGroup.GetField("length") - 1
            Dim Format As JavaObject = TrackGroup.RunMethodJO("getFormat", Array(j))
            Log(Format)
            Dim Metadata As JavaObject = Format.GetField("metadata")
            If Metadata.IsInitialized Then
                Log("Metadata: " & Metadata) 'will not show you anything
                
                'use below inline java method or use it with JavaObject
                Dim jo As JavaObject
                jo.InitializeContext
                Log(jo.RunMethod("GetMetaData",Array(Metadata)))
            End If
        Next
    Next
End Sub

#if java

import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame;

    //for tags, see this: http://id3.org/id3v2.4.0-frames
    
    public String GetMetaData(Metadata trackMetadata){
        String album = null;
        String title = null;
        String artist = null;
        for (int k = 0;k < trackMetadata.length();k++){
            Metadata.Entry entry = trackMetadata.get(k);
            if (entry instanceof TextInformationFrame){
                TextInformationFrame textInformationFrame = (TextInformationFrame) entry;
                String id = textInformationFrame.id;
                if (id != null && id.equals("TALB")){
                    album = textInformationFrame.value;
                } else if (id != null && id.equals(("TIT2"))) {
                    title = textInformationFrame.value;
                } else if (id != null && id.equals(("TPE1"))) {
                    artist = textInformationFrame.value;
                }
            }
        }

        if (album != null && title != null && artist != null){
            return album + "-" + title + "-" + artist;
        } else {
            return "no data";
        }

    }

#End If
 
Upvote 0

Almora

Active Member
Licensed User
Longtime User
While listening to the radio over the Internet, the track did not show its name.
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
@Almora I was under the impression it would work for MP3 radio stations, but unfortunately tags are only read when MP3 files are played locally.

Thanks anyway to all those who have helped!
 
Upvote 0
Top