Querying all audio files on an Android device yields no results

Carnagion 1 Reputation point
2021-10-05T09:22:11.567+00:00

This is my first time using Xamarin, so please bear with me.

I'm trying to create a music player app for Android that detects all music files on the user's device, lists them out, and lets the user play them.
This is part of the code I have for retrieving audio files:

string[] columns = {
    MediaStore.Audio.Media.InterfaceConsts.IsMusic,
    MediaStore.Audio.Media.InterfaceConsts.RelativePath,
    MediaStore.Audio.Media.InterfaceConsts.Title,
    MediaStore.Audio.Media.InterfaceConsts.Album,
    MediaStore.Audio.Media.InterfaceConsts.AlbumId,
    MediaStore.Audio.Media.InterfaceConsts.Artist,
    MediaStore.Audio.Media.InterfaceConsts.ArtistId,
};
Log.Info("Test", "Query start");
ICursor cursor = context?.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri, columns, null, null, null);
if (cursor is null)
{
    Log.Info("Test", "null");
    return;
}
while (cursor.MoveToNext())
{
    Log.Info("Test", "next");
    if (!Boolean.Parse(cursor.GetString(0)))
    {
        Log.Info("Test", "Not music");
        continue;
    }
    string trackPath = cursor.GetString(1);
    Log.Info("Test", trackPath);
}
Log.Info("Test", "Query end");

As a test, I uploaded 4 music files (.mp3) to my Android emulator and ran my app. The files are in SDCARD/Music.
Unfortunately, the query doesn't seem to contain any results. It prints out "Query start" and then immediately "Query end". From this I understand that it did not detect the files at all.

What am I doing wrong?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-10-06T06:10:51.3+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    When I use MediaStore.Audio.Media.InterfaceConsts.RelativePath I get an exception about RelativePath do not existed in android 9.0 emulator, then I change it to MediaStore.Audio.Media.InterfaceConsts.Data like following code.

       public List<string> GetAudioFile()  
               {  
                   List<string> vs = new List<string>();  
                 Context  context= Android.App.Application.Context;  
         
                   string[] columns = {  
                            MediaStore.Audio.Media.InterfaceConsts.IsMusic,  
                            MediaStore.Audio.Media.InterfaceConsts.Data,  
                            MediaStore.Audio.Media.InterfaceConsts.Title,  
                            MediaStore.Audio.Media.InterfaceConsts.Album,  
                            MediaStore.Audio.Media.InterfaceConsts.AlbumId,  
                            MediaStore.Audio.Media.InterfaceConsts.Artist,  
                            MediaStore.Audio.Media.InterfaceConsts.ArtistId,  
                             };  
                   Log.Info("Test", "Query start");  
                   ICursor cursor = context?.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri, columns, null, null, null);  
                   if (cursor is null)  
                   {  
                       Log.Info("Test", "null");  
                        
                   }  
                   while (cursor.MoveToNext())  
                   {  
                       Log.Info("Test", "next");  
                       //if (!Boolean.Parse(cursor.GetString(0)))  
                       //{  
                       //    Log.Info("Test", "Not music");  
                       //    continue;  
                       //}  
                    var Path=   cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data));  
                       string trackPath = cursor.GetString(1);  
                       vs.Add(trackPath);  
                       Log.Info("Test", trackPath);  
                   }  
                   Log.Info("Test", "Query end");  
         
                   return vs;  
         
         
               }  
    

    137994-image.png

    Note: If you want to read external file, you need to add following permission.

       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
    

    And grand the permission at the runtime.

       public class MainActivity : Activity  
           {  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   //Open read permission.  
                   if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)  
                   {  
                       RequestPermissions(new string[] { Manifest.Permission.ReadExternalStorage}, 0);  
                   }  
                    
               }  
       }  
    

    ==================
    Update code===================

    Here is my updated code.

       public List<string> GetAudioFile()  
               {  
                   List<string> vs = new List<string>();  
                 Context  context= Android.App.Application.Context;  
                   
                   string[] columns = {  
                            MediaStore.Audio.Media.InterfaceConsts.IsMusic,  
                            MediaStore.Audio.Media.InterfaceConsts.RelativePath,  
                             MediaStore.Audio.Media.InterfaceConsts.DisplayName,  
                            MediaStore.Audio.Media.InterfaceConsts.Title,  
                            MediaStore.Audio.Media.InterfaceConsts.Album,  
                            MediaStore.Audio.Media.InterfaceConsts.AlbumId,  
                            MediaStore.Audio.Media.InterfaceConsts.Artist,  
                            MediaStore.Audio.Media.InterfaceConsts.ArtistId,  
                             };  
                   Log.Info("Test", "Query start");  
                   ICursor cursor = context?.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri, columns, null, null, null);  
                   if (cursor is null)  
                   {  
                       Log.Info("Test", "null");  
                        
                   }  
                   while (cursor.MoveToNext())  
                   {  
                       Log.Info("Test", "next");  
         
                        var Path=   cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.RelativePath));  
                       string MP3Name = cursor.GetString(cursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.DisplayName));                       
                       string trackPath = Path + MP3Name;  
                       vs.Add(trackPath);  
                       Log.Info("Test", trackPath);  
                   }  
                   Log.Info("Test", "Query end");  
         
                   return vs;  
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.