/// <summary> /// 添加歌曲 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddSong(object sender, RoutedEventArgs e) { var menuItem = (MenuItem)sender; var contextMenu = (ContextMenu)menuItem.Parent; var items = (DataGrid)contextMenu.PlacementTarget; if (items.SelectedCells.Count == 0) { return; } var item = items.SelectedCells[0].Item as SongsSearchModule; if (item == null) { return; } InputDialog i = new InputDialog("歌曲名字:", "", "添加歌曲"); if (i.ShowDialog() == true && i.Answer != string.Empty) { SongItem song = item.SafeSearch(Config.MASTER_NAME, System.Web.HttpUtility.UrlEncode(i.Answer), Config.needLyric); if (song != null) { Center.AddSong(song); Center.Logg("手动添加歌曲成功:" + song.SongName); } } i = null; }
private static void onDownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { bool sucFlag = true; if (e.Cancelled) { Center.Logg("下载被取消"); Center.Mainw.setDownloadStatus("下载被取消"); sucFlag = false; try { File.Delete(dlItem.FilePath); } catch (Exception) { } } else if (e.Error != null) { Center.Logg("下载出错 " + e.Error.Message, true, true); Center.Mainw.setDownloadStatus("下载出错 " + e.Error.Message); sucFlag = false; } if (sucFlag) { Center.Logg("歌曲下载 下载成功:" + dlItem._SongName, false, true); Center.Mainw.setDownloadStatus("下载完毕"); dlItem.setStatus(SongItem.SongStatus.WaitingPlay); } else { Center.RemoveSong(dlItem); } DownloadWatchDog.Stop(); wc = null; downloadFlag = false; // 允许进行下一首歌的下载 }
internal static void Init() { new Thread(() => { while (true) { try { Thread.Sleep(1000); if (running && time != zero) { if ((DateTime.Now - time) >= new TimeSpan(0, 0, second)) { Center.Logg("下载速度长时间过慢,自动切断下载", true, true); DownloadControl.CancelDownload(); } } } catch (Exception) { } } }) { Name = "DownloadWatchDog", IsBackground = true }.Start(); }
private static void Download(SongItem item) { try { Directory.CreateDirectory(Config.SongsCachePath); } catch (Exception) { } dlItem = item; dlItem.setStatus(SongItem.SongStatus.Downloading); if (item.Module.HandleDownlaod) // 如果搜索模块负责下载文件 { Center.Mainw.setDownloadStatus("由搜索模块负责下载中"); switch (item.Module.SafeDownload(item)) { case 1: // 下载成功 dlItem.setStatus(SongItem.SongStatus.WaitingPlay); Center.Logg("歌曲下载 下载成功:" + item._SongName); Center.Mainw.setDownloadStatus("搜索模块返回下载成功"); return; case 0: // 下载失败 错误信息由module输出 default: Center.RemoveSong(item); Center.Mainw.setDownloadStatus("搜索模块返回下载失败"); return; } } else // 如果搜索模块不负责下载文件 { wc = new WebClient(); wc.DownloadProgressChanged += onDownloadProgressChanged; wc.DownloadFileCompleted += onDownloadFileCompleted; wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.450 Safari/537.35"); try { sw.Reset(); lastUpdateDownloadedSize = 0; lastUpdateTime = DateTime.Now; wc.DownloadFileAsync(new Uri(item._DownloadURL), item._FilePath); Center.Mainw.setDownloadStatus("正在连接服务器"); sw.Start(); DownloadWatchDog.Start(); downloadFlag = true; // 正在下载歌曲 } catch (Exception ex) { sw.Reset(); Center.Logg("下载歌曲" + item._SongName + "出错:" + ex.Message, true, true); Center.Mainw.setDownloadStatus("下载失败:" + ex.Message); } while (downloadFlag) { Thread.Sleep(500); } dlItem = null; } }
/// <summary> /// 从文件和播放列表里移除歌曲 /// </summary> /// <param name="song"></param> private static void RemoveSong(SongItem song) { try { File.Delete(song._FilePath); } catch (System.Exception ex) { Center.Logg("播放模块 删除歌曲缓存失败:" + ex.Message); } Center.RemoveSong(song); }