Retrieve MetaData from audio file in Android

0

I want to retrieve metadata(artist, album, path, name, length) for songs on my device. This is the code that I'm using.

 String selection = MediaStore.Audio.Media.InterfaceConsts.IsMusic + " != 0";

        String[] projection = {
                MediaStore.Audio.Media.InterfaceConsts.Artist,                  //artist
                MediaStore.Audio.Media.InterfaceConsts.Album,                   //album
                MediaStore.Audio.Media.InterfaceConsts.Data,                    //path
                MediaStore.Audio.Media.InterfaceConsts.DisplayName,             //title
                MediaStore.Audio.Media.InterfaceConsts.Duration
                };

        var cursor = myContentResolver.Query(
                MediaStore.Audio.Media.ExternalContentUri,
                projection,
                selection,
                null,
                null);


        while (cursor.MoveToNext())
        {
            string path = cursor.GetString(2); string name = cursor.GetString(3); string artist =  cursor.GetString(0); string album cursor.GetString(1); string duration = cursor.GetString(4);

        }

The problem with this code is that length of the song is not correct. For some songs it returns a shorter time than it should. So I use a piece of code only for getting length of the song. This is the code:

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.SetDataSource(path);  //path is the path of the song file
        lenth = mmr.ExtractMetadata(MetadataKey.Duration);

But there is another problem with this code. For some songs I get an exception at line 2:

Java.Lang.RuntimeException: setDataSource failed: status = 0x80000000

I also want to say that I am using the permissions:

Manifest.Permission.ReadExternalStorage,
 Manifest.Permission.Internet

I am using Xamarin.Android for my project.

android
audio
xamarin.android
metadata
asked on Stack Overflow Mar 24, 2018 by Raducu Mihai

1 Answer

0

This Code Works

public List getAllAudioFromDevice(final Context context) {

    final List<MusicFilesModal> tempAudioList = new ArrayList<>();

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM,
            MediaStore.Audio.ArtistColumns.ARTIST,
            MediaStore.Audio.Media.DURATION};


    Cursor c = context.getContentResolver().query(uri, projection, null, null, null);

    if (c != null) {
        while (c.moveToNext()) {

            String path = c.getString(0);
            String album = c.getString(1);
            String artist = c.getString(2);
            String duration = c.getString(3);

            String title = path.substring(path.lastIndexOf("/") + 1);

            MusicFilesModal audioModel = new MusicFilesModal(path, title, artist, album, duration);

            //Log.e("Name :" + title, " Album :" + album);
            //Log.e("Path :" + path, " Artist :" + artist);

            Log.d("artistName_", artist);
            Log.d("albumName_", album);

            tempAudioList.add(audioModel);
        }
        c.close();
    }
    Log.d("tempAudioListSize_", tempAudioList.size() + "_");

    return tempAudioList;
}
answered on Stack Overflow Nov 11, 2020 by asad ghaffar

User contributions licensed under CC BY-SA 3.0