WQYeo
WQYeo

Reputation: 4056

Get Music files details only

Basically I am trying to get the details of a music file inside an android device (Ex. The music's title, artist, etc) and store them to a list<>.
(SongData contains the list, and it contains a list of Song object.)

public void PopulateSongList(Context context) {
        SongData songData = new SongData();
        Android.Net.Uri musicUri = Android.Provider.MediaStore.Audio.Media.ExternalContentUri;
        //Used to query the media files.
        ICursor musicCursor = context.ContentResolver.Query(musicUri, null, null, null, null);

        if (musicCursor != null && musicCursor.MoveToFirst())
        {
            int songID = 0;

            //get columns.
            int songTitleColumn = musicCursor.GetColumnIndex("title");
            int songArtistColumn = musicCursor.GetColumnIndex("artist");

            //add songs to list
            do
            {
                String songTitle = musicCursor.GetString(songTitleColumn);
                //Tried, but file name does not contain file extension.
                if (songTitle.ToLower().Contains(".mp4") || songTitle.ToLower().Contains(".m4a"))
                {
                    String songArtist = musicCursor.GetString(songArtistColumn);
                    songData.AddNewSong(new Song(++songID, songTitle, songArtist, null));
                }
            }
            while (musicCursor.MoveToNext());
        }
    }

The code works well but however in the do..while loop, songTitle also contains other files that are not music files as well.
I tried checking for file extensions but it does not work, and after using the debugger again, the file name given does not contain the file extension.

So how do I make sure that it only grabs details from a music file, and not other kind of files?
(Also, I want to grab the name of the artist of a music file, but apparently int songArtistColumn = musicCursor.GetColumnIndex("artist"); does not seem to work.)

Finally, how do I get the image of a music file? (The song image).

Upvotes: 1

Views: 189

Answers (1)

Limitless_ZA
Limitless_ZA

Reputation: 66

You can try to use musicCursor.GetColumnNames() to see what the file is allowing you to work with.

Here's an image of the possible results you can get if you execute it. musicCursor.GetColumnNames() will return the results in a string[] so you can use List<string> songInfo = new List<string>(string[]);

A problem you might get is a NullException, this happens when the data you're trying to work with have a value of null. You'll get this value directly from the file if the Artist or other values are unknown.

I don't know where the Image file is stored, but if it's stored with the music file it will most likely be stored in a byte[]. So you'll need to convert the byte[] to an image.

Upvotes: 1

Related Questions