【问题标题】:Get Music files details only仅获取音乐文件详细信息
【发布时间】:2017-09-12 16:31:43
【问题描述】:

基本上,我正在尝试获取 android 设备中音乐文件的详细信息(例如,音乐的标题、艺术家等)并将它们存储到 list<>
SongData 包含列表,它包含Song 对象的列表。)

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());
        }
    }

代码运行良好,但在do..while 循环中,songTitle 还包含其他不是音乐文件的文件。
我尝试检查文件扩展名但它不起作用,再次使用调试器后,给出的文件名不包含文件扩展名。

那么如何确保它只从音乐文件中获取细节,而不是其他类型的文件呢?
(另外,我想获取一个音乐文件的艺术家的名字,但显然int songArtistColumn = musicCursor.GetColumnIndex("artist"); 似乎不起作用。)

最后,如何获取音乐文件的图像? (歌曲图片)。

【问题讨论】:

    标签: c# android xamarin xamarin.android


    【解决方案1】:

    您可以尝试使用musicCursor.GetColumnNames() 查看该文件允许您使用什么。

    Here's an image of the possible results执行就可以获得。 musicCursor.GetColumnNames() 将在 string[] 中返回结果,因此您可以使用 List<string> songInfo = new List<string>(string[]);

    您可能遇到的问题是NullException,当您尝试使用的数据的值为 null 时会发生这种情况。如果 Artist 或其他值未知,您将直接从文件中获取此值。

    我不知道图像文件存储在哪里,但如果它与音乐文件一起存储,它很可能存储在byte[] 中。因此,您需要将byte[] 转换为图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 2012-01-04
      • 2016-12-07
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      相关资源
      最近更新 更多