Esempio n. 1
0
        public override async Task <ReturnObj <List <MusicMetadataItem> > > GetMusicAsync()
        {
            try
            {
                var context = Android.App.Application.Context;

                var musicUri = MediaStore.Audio.Media.ExternalContentUri;

                var musicCursor = context.ContentResolver.Query(musicUri,
                                                                new[] {
                    MediaStore.Audio.AudioColumns.Artist,
                    MediaStore.Audio.AudioColumns.Title,
                    MediaStore.Audio.AudioColumns.Album,
                    MediaStore.Audio.AudioColumns.Duration,
                }, null, null, null);

                var musicList = new List <MusicMetadataItem>();

                if (musicCursor.MoveToFirst())
                {
                    do
                    {
                        try
                        {
                            var track = new MusicMetadataItem
                            {
                                Artist   = musicCursor.GetStrProperty(MediaStore.Audio.AudioColumns.Artist),
                                Name     = musicCursor.GetStrProperty(MediaStore.Audio.AudioColumns.Title),
                                Album    = musicCursor.GetStrProperty(MediaStore.Audio.AudioColumns.Album),
                                Duration = TimeSpan.FromMilliseconds(
                                    musicCursor.GetLongProperty(MediaStore.Audio.AudioColumns.Duration))
                            };

                            musicList.Add(track);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex);
                        }
                    } while (musicCursor.MoveToNext());
                }

                musicCursor.Close();

                return(new ReturnObj <List <MusicMetadataItem> >(musicList));
            }
            catch (Exception ex)
            {
                return(new ReturnObj <List <MusicMetadataItem> >(ex));
            }
        }
        public override async Task <ReturnObj <List <MusicMetadataItem> > > GetMusicAsync()
        {
            try
            {
                var queryOption = new QueryOptions(CommonFileQuery.OrderByTitle, new[] { ".mp3", ".mp4", ".wma", ".flac" })
                {
                    FolderDepth = FolderDepth.Deep
                };

                var files = await KnownFolders.MusicLibrary.CreateFileQueryWithOptions(queryOption).GetFilesAsync();

                var musicList = new List <MusicMetadataItem>();

                foreach (var file in files)
                {
                    try
                    {
                        var musicProperties = await file.Properties.GetMusicPropertiesAsync();

                        var track = new MusicMetadataItem
                        {
                            Artist           = musicProperties.AlbumArtist,
                            Name             = musicProperties.Title,
                            Album            = musicProperties.Album,
                            Duration         = musicProperties.Duration,
                            AlbumReleaseYear = (int?)musicProperties.Year,
                            TrackNumber      = (int)musicProperties.TrackNumber
                        };

                        musicList.Add(track);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                    }
                }

                return(new ReturnObj <List <MusicMetadataItem> >(musicList));
            }
            catch (Exception ex)
            {
                return(new ReturnObj <List <MusicMetadataItem> >(ex));
            }
        }