/// <summary> /// Download song into the device a-synchronicly /// </summary> /// <param name="song"></param> public static void DownloadAndSaveSongAsync(SongModel song) { if (song.IsLoaded) return; WebClient webClient = new WebClient(); //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged); //Set Callback action on Read Complete webClient.OpenReadCompleted += (sender, args) => { webClient_OpenReadCompleted(sender, args, song); }; //Start Read Async webClient.OpenReadAsync(song.ServerURI); }
/// <summary> /// Action to take when download completed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="song"></param> private static void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e, SongModel song) { try { if (e.Result != null) { IsolatedStorageFileStream isolatedStorageFileStream; IsolatedStorageFile isolatedStorageFile; //TODO: Check that file save os done correctly #region Isolated Storage Copy Code isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); bool checkQuotaIncrease = FilesManager.CanIsolatedStorageSpaceSizeIncrease(e.Result.Length); string VideoFile = fileDirectory + "\\" + song.Id.ToString(); isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile); long VideoFileLength = (long)e.Result.Length; byte[] byteImage = new byte[VideoFileLength]; e.Result.Read(byteImage, 0, byteImage.Length); isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length); isolatedStorageFileStream.Close(); #endregion //Notify song is loaded song.IsLoaded = true; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// This function loads song list into songs Collection. /// </summary> private void LoadSongList(List<SongResponse> songsResponse) { foreach (var s in songsResponse) { bool exists = SongManager.CheckIfSongIsLoaded(s.id); //Download song from server with DownloadSongComplete callback //if (exists) //TODO: Remove this later //{ //Create corresponding SongModel var songModel = new SongModel(s.id, s.artistName, s.name, LengthToString(s.length), string.Empty, new Uri(s.playUrl), exists); //TODO: Get Song Picture songs.Add(songModel); //} } }