Search code examples
androidtagsid3mp3agic

How to get ID3 Lyrics tag from MP3 file using Mp3agic jar Android studio?


I need to get the lyrics from mp3 song

Mp3File mp3file = new Mp3File(file);
       
if (mp3file.hasId3v2Tag()) {

   ID3v2 id3v2Tag = mp3file.getId3v2Tag();
   Log.d("Lyrics",id3v2Tag.getLyrics());
}

The above code only I'm using. But its returning "java.lang.NullPointerException". Please help me to solve this issue.

The full crash report

E/AndroidRuntime: FATAL EXCEPTION: main Process: in.example.mp3tag, PID: 10237 java.lang.RuntimeException: Unable to start activity ComponentInfo{in.example.mp3tag/in.example.mp3tag.Main2Activity}: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:206) at android.app.ActivityThread.main(ActivityThread.java:6784) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.d(Log.java:320) at in.example.mp3tag.Main2Activity.onCreate(Main2Activity.java:200) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)  at android.os.Handler.dispatchMessage(Handler.java:108)  at android.os.Looper.loop(Looper.java:206)  at android.app.ActivityThread.main(ActivityThread.java:6784)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852) 


Solution

  • You're getting an exception because id3v2Tag.getLyrics() returned null and you're using Log.d(String tag, String msg). See documentation regarding this method here.

    public static int d (String tag, String msg) ... String: The message you would like logged. This value cannot be null.

    If you want to use Log.d with possible null objects, use Log.d(String tag, String msg, Throwable tr). See documentation here.

    So id3v2Tag.getLyrics() returns null. The library you're using offers little documentation but I'm assuming getLyrics() returns null in the absence of lyrics, and not an empty string. This may not be an issue for the rest of your code if you test the result of getLyrics() properly, like so:

    String lyrics = id3v2Tag.getLyrics();
    if (lyrics != null) {
      // Lyrics found, add your code here
    } else {
      // No lyrics
    }