I have a method that makes a list of all the Songs in the Media Library. The method has 2 aspects it either returns the SongsCollection from the MediaLibrary or returns a list of custom objects.
public static Task<object> GetSongList(bool lib = true, bool albumArt = true) { MediaLibrary mediaLib = new MediaLibrary(); var songs = mediaLib.Songs; if (lib) { return songs; } else { var list = new List<MusicTitle>(); foreach (var song in songs) { list.Add(new MusicTitle() { Artist = song.Artist.Name, Title = song.Name, Duration = (new DateTime(song.Duration.Ticks)).ToString("mm:ss"), Album = song.Album.Name, Art = albumArt ? GetAlbumArt(song, 100) : null }); } return list; } } MusicTitle is the custom class with some properties. since this return 2 types of results i set the return type as object and cast the result appropriately. This holds up the UI for a bit so i need this to be async. So as the methods shows i added Task<object> to the method signature and when i do that return songs; and return lists; gives compile following compile errors.
Cannot implicitly convert type 'Microsoft.Xna.Framework.Media.SongCollection' to 'System.Threading.Tasks.Task<object>' Cannot implicitly convert type 'System.Collections.Generic.List<KVKWindowsPhoneHelper.Core.MediaLibrary.MusicTitle>' to 'System.Threading.Tasks.Task<object>' What should i do? I tried casting the return types to object but did not work. How can i make this method async?
EDIT
The Following code is in side the Page's OnNavigatedTo() method This throws the UnauthorizedAccessException
protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); // Set Song List List<AlphaKeyGroup<MusicTitle>> songList = await Task.Run(() => AlphaKeyGroup<MusicTitle>.CreateGroups(ListHelper.GetSongList(false, false) as List<MusicTitle>, Thread.CurrentThread.CurrentUICulture, (MusicTitle s) => { return s.Title; }, true)); listSongs.ItemsSource = songList; // Set Artist List List<AlphaKeyGroup<MusicArtist>> artistList = await Task.Run(() => AlphaKeyGroup<MusicArtist>.CreateGroups(ListHelper.GetArtistList(false) as List<MusicArtist>, Thread.CurrentThread.CurrentUICulture, (MusicArtist ar) => { return ar.Artist; }, true)); listArtist.ItemsSource = artistList; // Set Album List List<AlphaKeyGroup<MusicAlbum>> albumList = await Task.Run(() => AlphaKeyGroup<MusicAlbum>.CreateGroups(ListHelper.GetAlbumList(false, true) as List<MusicAlbum>, Thread.CurrentThread.CurrentUICulture, (MusicAlbum al) => { return al.Album; }, true)); listAlbums.ItemsSource = albumList; // Set Genre List List<AlphaKeyGroup<MusicGenre>> genreList = await Task.Run(() => AlphaKeyGroup<MusicGenre>.CreateGroups(ListHelper.GetGenreList(false) as List<MusicGenre>, Thread.CurrentThread.CurrentUICulture, (MusicGenre g) => { return g.Genre; }, true)); listGenres.ItemsSource = genreList; // Set PlayList List<AlphaKeyGroup<MusicPlaylist>> playList = await Task.Run(() => AlphaKeyGroup<MusicPlaylist>.CreateGroups(ListHelper.GetPlayList(false) as List<MusicPlaylist>, Thread.CurrentThread.CurrentUICulture, (MusicPlaylist pl) => { return pl.Playlist; }, true)); listPlaylist.ItemsSource = playList; } EDIT 2
In my Phone there are 233 songs, The exception is thrown at a method in a ListHelper class i wrote that includes all the methods to get the Songs, Albums, Playlist, Genres and Artists. The method that throws the exception is the method where i get the Album Art for the Album. This is the Method.
public static BitmapImage GetAlbumArt(Song song, int size = 100 ) { BitmapImage img = new BitmapImage(); // EXCEPTION IS THROWN HERE img.DecodePixelHeight = size; img.DecodePixelWidth = size; if (song.Album.HasArt) { img.SetSource(song.Album.GetAlbumArt()); } else { img.UriSource = new Uri("/Images/cover.png", UriKind.Relative); } return img; }