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.

Raducu Mihai
  • 313
  • 4
  • 14
  • What's the version of Android are you testing? According to this [post](https://stackoverflow.com/questions/16395559/mediaplayer-setdatasource-failed-with-status-0x80000000-for-ringtone-set-by-file) the exception could caused by file path. – Billy Liu - MSFT Mar 26 '18 at 10:02
  • I am using Android 8.0 for testing on my Samsung Galaxy S8+. – Raducu Mihai Mar 27 '18 at 10:06

1 Answers1

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;
}
  • 1
    Please explain your code a little bit more in detail, thanks. – Lukas Nov 11 '20 at 15:31
  • First You have to create a array list in which you can pass your modal list. Then You can Fetch the path of media store in a string type variable. Then create a query in which you can add the data details like fetch the audio name, artist name etc. then pass the query to the cursor. Then create if statemnt and get all the data (audio, name, duration etc) in a loop and fetch all the data in variable and give those variables to modal list and add the data to modal list – asad ghaffar Nov 11 '20 at 19:23