private static void ProgressCallback(DownloadOperation obj) { if (obj.Progress.BytesReceived == obj.Progress.TotalBytesToReceive) { if (!DownloadLists.ContainsKey(obj)) { return; } _ = Task.Run((async() => { //下载歌词 (bool isOk, Newtonsoft.Json.Linq.JObject json) = await Common.ncapi.RequestAsync(CloudMusicApiProviders.Lyric, new Dictionary <string, object>() { { "id", DownloadLists[obj].sid } }); if (isOk) { if (!(json.ContainsKey("nolyric") && json["nolyric"].ToString().ToLower() == "true") && !(json.ContainsKey("uncollected") && json["uncollected"].ToString().ToLower() == "true")) { var sf = await(await StorageFolder.GetFolderFromPathAsync(Common.Setting.downloadDir)).CreateFileAsync( Path.GetFileName(Path.ChangeExtension(obj.ResultFile.Path, "lrc")), CreationCollisionOption.ReplaceExisting); var lrc = Utils.ConvertPureLyric(json["lrc"]["lyric"].ToString()); Utils.ConvertTranslation(json["tlyric"]["lyric"].ToString(), lrc); var lrctxt = string.Join("\r\n", lrc.Select(t => { if (t.HaveTranslation) { return("[" + t.LyricTime.ToString(@"mm\:ss\.ff") + "]" + t.PureLyric + " 「" + t.Translation + "」"); } return("[" + t.LyricTime.ToString(@"mm\:ss\.ff") + "]" + t.PureLyric); })); await FileIO.WriteTextAsync(sf, lrctxt); } } //写相关信息 var file = TagLib.File.Create(new UwpStorageFileAbstraction((StorageFile)obj.ResultFile)); file.Tag.Album = DownloadLists[obj].Album.name; file.Tag.Performers = DownloadLists[obj].Artist.Select(t => t.name).ToArray(); file.Tag.Title = DownloadLists[obj].songname; file.Tag.Pictures = new IPicture[] { new Picture(ByteVector.FromStream(RandomAccessStreamReference.CreateFromUri(new Uri(DownloadLists[obj].Album.cover)).OpenReadAsync().GetAwaiter().GetResult().AsStreamForRead())) }; The163KeyHelper.TrySetMusicInfo(file.Tag, DownloadLists[obj]); file.Save(); ToastContent downloadToastContent = new ToastContent() { Visual = new ToastVisual() { BindingGeneric = new ToastBindingGeneric() { Children = { new AdaptiveText() { Text = "下载完成", HintStyle = AdaptiveTextStyle.Header }, new AdaptiveText() { Text = string.Join(';', DownloadLists[obj].Artist.Select(t => t.name).ToArray()) + " - " + DownloadLists[obj].songname } } } }, Launch = "", Scenario = ToastScenario.Reminder, Audio = new ToastAudio() { Silent = true } }; var toast = new ToastNotification(downloadToastContent.GetXml()) { Tag = "HyPlayerDownloadDone", Data = new NotificationData() }; toast.Data.SequenceNumber = 0; ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier(); notifier.Show(toast); //清理 DownloadLists.Remove(obj); })); } }
public static async Task <bool> AppendFile(StorageFile sf, bool nocheck163 = false) { The163KeyStruct mi; if (nocheck163 || !The163KeyHelper.TryGetMusicInfo(TagLib.File.Create(new UwpStorageFileAbstraction(sf)).Tag, out mi)) { //TagLib.File afi = TagLib.File.Create(new UwpStorageFileAbstraction(sf), ReadStyle.Average); var mdp = await sf.Properties.GetMusicPropertiesAsync(); string[] contributingArtistsKey = { "System.Music.Artist" }; IDictionary <string, object> contributingArtistsProperty = await mdp.RetrievePropertiesAsync(contributingArtistsKey); string[] contributingArtists = contributingArtistsProperty["System.Music.Artist"] as string[]; if (contributingArtists is null) { contributingArtists = new[] { "未知歌手" }; } AudioInfo ai = new AudioInfo() { tag = "本地", Album = string.IsNullOrEmpty(mdp.Album) ? "未知专辑" : mdp.Album, ArtistArr = contributingArtists, Artist = string.IsNullOrEmpty(string.Join('/', contributingArtists)) ? "未知歌手" : string.Join('/', contributingArtists), LengthInMilliseconds = mdp.Duration.TotalMilliseconds, SongName = string.IsNullOrEmpty(mdp.Title) ? sf.DisplayName : mdp.Title, LocalSongFile = sf }; //记载歌词 try { StorageFile lrcfile = await(await sf.GetParentAsync()).GetFileAsync(Path.ChangeExtension(sf.Name, "lrc")); ai.Lyric = await FileIO.ReadTextAsync(lrcfile); } catch (Exception) { } HyPlayItem hyPlayItem = new HyPlayItem() { AudioInfo = ai, isOnline = false, ItemType = HyPlayItemType.Local, Name = ai.SongName, Path = sf.Path }; List.Add(hyPlayItem); return(true); } else { if (string.IsNullOrEmpty(mi.musicName)) { return(await AppendFile(sf, true)); } NCPlayItem hpi = new NCPlayItem() { Album = new NCAlbum() { name = mi.album, id = mi.albumId.ToString(), cover = mi.albumPic }, bitrate = mi.bitrate, hasLocalFile = true, LocalStorageFile = sf, LengthInMilliseconds = mi.duration, sid = mi.musicId.ToString(), Artist = null, md5 = null, size = sf.GetBasicPropertiesAsync().GetAwaiter().GetResult().Size.ToString(), songname = mi.musicName, tag = "本地" }; hpi.Artist = mi.artist.Select(t => new NCArtist() { name = t[0].ToString(), id = t[1].ToString() }) .ToList(); AppendNCPlayItem(hpi); return(true); } }