Forum Xamarin.Forms

List all .mp3 files from Music folder of my Android device

serkyserky Member

Hi,
I am developing a music player app in Xamarin.Forms that uses MediaManager plugin. How can I list all .mp3 files from Music folder in my Android device and play it respectively. Also, can I get metadatas of an .mp3 files name, title, album, artist etc?

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For the music folder, you can use the following code to get the path :

    string musicDirectoryPath= 
    Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).ToString();
    

    If you want to list all .mp3 files from Music folder and its subdirectories in your Android device , you can try following code:

    foreach (string musicFilePath in Directory.EnumerateFiles(musicDirectoryPath, "*", SearchOption.AllDirectories)) {
            Log.Info("Music File", "Music file path is " + musicFilePath);
            // do whatever you want to with the file here
    }
    

    Note: you can change that " * " in the method call to ".mp3" or ".wav" or so on, to only match files with those extensions.

  • jezhjezh Member, Xamarin Team Xamurai

    Also, can I get metadatas of an .mp3 files name, title, album, artist etc?

    For this question, you can refer to the following code:

    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.SetDataSource(filePath);
    
    string albumName = mmr.ExtractMetadata(Android.Media.MetadataKey.Album);
    string Author = mmr.ExtractMetadata(Android.Media.MetadataKey.Author);
    

    For more details, you can check: https://developer.android.com/reference/android/media/MediaMetadataRetriever

  • jezhjezh Member, Xamarin Team Xamurai

    Hi serky, how are you doing? I’m writing to check if provided information is helpful. Please let me know if you need further assistance. Thank you.

Sign In or Register to comment.