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.