// Returns track by index public DeezerTrack getTrack(int index) { try { return(search_results.data[index]); } catch { DeezerTrack bad_result = new DeezerTrack(); return(bad_result); } }
// Adding track to a playlist // 0 : no errors // 1 : default DeezerTrack object // 2 : track already in playlist // 42: get string error to have message public int addTrackToPlaylist(DeezerTrack src_track, int user_index, int playlist_index) { int status = 0; try { // Check if given track object is not just initialized if (src_track.Equals(default(DeezerTrack))) { status = 1; } else { // Check if track isn't already in the playlist bool is_track_already_here = false; foreach (DeezerTrack track in database.user[user_index].playlists[playlist_index].tracklist) { if (src_track.id == track.id) { is_track_already_here = true; } } // If track doesn't exists in playlist if (is_track_already_here == false) { // Adding it database.user[user_index].playlists[playlist_index].tracklist.Add(src_track); // Saving changes in database saveDatabase(); } else { status = 2; } } } // If something else happend catch (Exception ex) { error = ex.Message; status = 42; } return(status); }
// Reads track preview if it exists public async void readPreview(DeezerTrack track) { try { // Check if given track is not just initialized if (track.Equals(default(DeezerTrack))) { error = "Given track is only initialized (default value)"; } else { string track_preview_file = track.id.ToString() + ".mp3"; try { track_file = await ApplicationData.Current.LocalFolder.GetFileAsync(track_preview_file); download_status = 4; playMusic.Stop(); playMusic.SetSource(await track_file.OpenAsync(FileAccessMode.Read), track_file.ContentType); playMusic.Play(); } catch { // Checking if the folder is valid if (ApplicationData.Current.LocalFolder != null) { // file object track_file = await ApplicationData.Current.LocalFolder.CreateFileAsync(track_preview_file, CreationCollisionOption.ReplaceExisting); // starts the download dlOperation = bDownloader.CreateDownload(new Uri(track.preview), track_file); // used to display download progression Progress <DownloadOperation> progression = new Progress <DownloadOperation>(progressChanged); CancellationTokenSource cancellationToken = new CancellationTokenSource(); try { download_status = 2; await dlOperation.StartAsync().AsTask(cancellationToken.Token, progression); } catch (Exception ex) { download_status = 3; await dlOperation.ResultFile.DeleteAsync(); error = ex.Message; dlOperation = null; } } else { download_status = 1; } } } } catch (Exception ex) { error = ex.Message; } }