Declare help, reading media tags

Hi all, I’m trying to read mp3 tags, I’ve tried declares with no joy, here is the code, can anyone fix it ?..

dim mp3FilePath as CString = "/path /to /mp3 file/"

Declare Function MediaMetadataRetriever Lib "android.media" () As Ptr
Declare Sub setDataSource Lib "android.media.MediaMetadataRetriever" (retriever As Ptr, path As cString)
Declare Function extractMetadata Lib "android.media.MediaMetadataRetriever" (retriever As Ptr, keyCode As Int32) As cString
Declare Sub releaseMediaMetadataRetriever Lib "android.media.MediaMetadataRetriever" (retriever As Ptr)

' Constants for metadata keys
Const METADATA_KEY_TITLE As Int32 = 7
Const METADATA_KEY_ARTIST As Int32 = 2
Const METADATA_KEY_ALBUM As Int32 = 1
Const METADATA_KEY_DURATION As Int32 = 9

' Method to access MP3 media tags
'Function GetMP3Tags(mp3FilePath As String) As Dictionary
Dim tags As New Dictionary

' Create a new instance of MediaMetadataRetriever
Dim retriever As Ptr = MediaMetadataRetriever()

Try
  ' Set the data source to the MP3 file
  setDataSource(retriever, mp3FilePath)
  
  ' Retrieve various MP3 tags
  Dim title As String = extractMetadata(retriever, METADATA_KEY_TITLE)
  Dim artist As String = extractMetadata(retriever, METADATA_KEY_ARTIST)
  Dim album As String = extractMetadata(retriever, METADATA_KEY_ALBUM)
  Dim duration As String = extractMetadata(retriever, METADATA_KEY_DURATION)
  
  ' Store the retrieved tags in the dictionary
  tags.Value("title") = title
  tags.Value("artist") = artist
  tags.Value("album") = album
  tags.Value("duration") = duration
  
Catch e As RuntimeException
  ' Handle exceptions
  MessageBox("Error: " + e.Message)
End Try

When posting code it would be good to highlight it and press the </> button. It will then be formatted and easier for people to read.

I’m not sure what permissions are like on Android, do you have permission to open and read from the file?

Hi Ian, thanks for the tip, i didn’t know you could hilight the code :slight_smile:
Dont know about permissions but the code wont compile, no matter what I change it says the library doesnt exist or syntax error, Im new to declare’s and a bit stumped with this…

You won’t have any luck with that. The use of Ptr in Android declares is only possible as a parameter and only in conjunction with Android libraries. Xojo does not currently offer more Ptr support on Android.

Here is the code for an Android library that you can build based on the Creating an Android Library with Kotlin for use with Xojo tutorial. It works well, as you can see in the screenshot. You can then read the returned JSON string into a Xojo JSONItem and process it further.

package com.xojo.utility

import org.json.JSONObject
import android.media.MediaMetadataRetriever

class test {
  companion object {
    fun mp3(filePath: String?): String? {
        val retriever = MediaMetadataRetriever()

        try {
           retriever.setDataSource(filePath)
           val json = JSONObject()

           val metadataKeys = mapOf(
                 MediaMetadataRetriever.METADATA_KEY_ALBUM to "album",
                 MediaMetadataRetriever.METADATA_KEY_ARTIST to "artist",
                 MediaMetadataRetriever.METADATA_KEY_DURATION to "duration",
                 MediaMetadataRetriever.METADATA_KEY_GENRE to "genre",
                 MediaMetadataRetriever.METADATA_KEY_TITLE to "title"
           )

           for ((key, value) in metadataKeys) {
                json.put(value, retriever.extractMetadata(key))
           }

           return json.toString()
           } catch (e: Exception) {
               e.printStackTrace()
               return "{}"
           } finally {
               retriever.release()
           }
       }
    }
}

And the code for the call in your Xojo app:

Var file As FolderItem = SpecialFolder.Resource("sample-file-2.mp3")

Declare Function mp3 Lib "com.xojo.utility.test" (value As CString) As CString
Var metadata As String = mp3(file.NativePath)

MessageBox(metadata)

2 Likes

Hi Martin, the code looks like its written in C or something (curly braces), where would I put that code in Xojo ?

It’s Kotlin. Please read the linked blog article, then you can follow my instructions.

Thanks Martin, I missed that link on first reading I was focused on the code, I will give it a go :slight_smile:

1 Like

Well, I tried lol, android studio is impossible to work with for someone like me who has never used it before.
Is there any way to read tags just using Xojo ?

You could use the hard way and parse the whole MP3 file using BinaryStream. But I wouldn’t do that.

Did you followed the tutorial, did you try? It’s pretty easy, shouldn’t be that hard. The tutorial shows you step by step what you need to do in Android Studio.

Yeah I followed the tutorial but it references “scripts” and “navigators” but doesnt tell you ‘how’ or where to open them, however I think i nearly did it but when it got to the part about changing the version number near the end i couldnt find anything that loos like the file in the tutorial. the tutorial is a year old so maybe android studio has changed since it was written. I did download the example but it was giving errors and asking if i want to upgrade some files or something, I’m out of my depth with android studio as I’m just a weekend-warrior.

I think if you have to keep going to android studio then it kind of makes Xojo a bit of a non starter for android projects, for dunce’s like me i think the xojo team whould need to maybe make a load of android classes that we could use in our projects, a bit like monkeybread software has a library of usefull classes, dunno just an idea…

Martin, would it be possible for you to share the developed / compiled library to Steve?

Ok, so I had another go today and the tutorial is out of date, the instructions don’t match the current android studio interface, however I did eventually get it to build but with a few problems, the compiler wont accept kotlin 1.6.20 it says its an incompattible version, so I built with default version 1.9.0 and it built ok, but I cannot find any ‘aar’ file in the output folder ?.

Any help would be appreciated as I’m stuck with this… :-/

@Steve_Dorn - I have built you the Android Library and also the Xojo sample project.

If no tags are found, an empty JSON String {} is returned, otherwise the method supports the following tags:

  • album
  • artist
  • duration
  • albumartist
  • author
  • bitrate
  • captureframerate
  • compilation
  • composer
  • date
  • discnumber
  • genre
  • year
  • duration
  • genre
  • title

Good luck.

1 Like

Thanks for your help Martin, however I’m unable to log into the google drive to download it ?.

@Steve_Dorn - Sorry about that. Link should now work.

It works!, so happy, thank you so much Martin :slight_smile:

1 Like