private static void OnDownloadProgressChange(object sender, DownloadProgressChangeEventArgs e) { if ((DateTime.Now - _lastUpdateProgress).TotalSeconds < 2) { _lastUpdateProgress = DateTime.Now; return; } MediaFileDownload downloader = sender as MediaFileDownload; DownFactory.UpdateFileLenRecord(downloader.Tag as string, downloader.FileLength); foreach (DownInfo info in MyDownloading) { if (downloader.Tag as string == info.VID) { info.ProgressText = String.Format("正在下载:{0}M/{1}M", ((double)e.Progress / (double)(1024 * 1024)).ToString("f2"), ((double)e.TotalBytes / (double)(1024 * 1024)).ToString("f2")); info.ProgressNumeric = (double)e.Progress / (double)e.TotalBytes * 100; break; } } foreach (DBMyDownloadInfo downInfo in DownFactory.GetAllRecords()) { if (downInfo.VID == downloader.Tag as string) { if ((DateTime.Now.Second - _lastWriteDB.Second) >= _minDBOperationInterval) { DownFactory.UpdateRecord(downloader.Tag as string, -1, e.Progress); _lastWriteDB = DateTime.Now; } break; } } }
public static bool AddTask(ChannelDetailInfo channelDetailInfo, ProgramInfo programInfo) { MediaFileDownload downloader = new MediaFileDownload(); string index = (programInfo != null) ? programInfo.Index.ToString() : string.Empty; string channelVid = (programInfo != null) ? programInfo.ChannelVID.ToString() : string.Empty; downloader.Tag = channelDetailInfo.VID.ToString() + "|" + index + "|" + channelVid; if (DownFactory.IsExistVid(downloader.Tag as string)) { return(false); } Utils.MediaFileDownload.Add(downloader); int rate = (int)channelDetailInfo.Mark; string subTitle = string.Empty; if (channelDetailInfo.TypeID != 1 && programInfo != null) { subTitle = "(第" + (programInfo.Index + 1).ToString() + "集)"; } DownFactory.InsertRecord(downloader.Tag as string, channelDetailInfo.ImgUrl, channelDetailInfo.Title, "", downloader.PathName, 0, 0, (int)DownloadStatus.Downloading, rate); DownInfo info = new DownInfo(downloader.Tag as string, channelDetailInfo.ImgUrl, "正在下载:0M", 0, channelDetailInfo.Title, subTitle, "/PPTVForWP7;component/Images/Pause.png", rate, String.Empty); MyDownloading.Add(info); foreach (MediaFileDownload down in Utils.MediaFileDownload.DownloadQueue) { down.DownloadStatusChange += OnDownloadStatusChange; down.DownloadProgressChange += OnDownloadProgressChange; } return(true); }
private static void OnDownloadStatusChange(object sender, DownloadStatusChangeEventArgs e) { MediaFileDownload downloader = sender as MediaFileDownload; string statusImage = string.Empty; foreach (DownInfo info in MyDownloading) { if (info.VID == downloader.Tag as string) { switch (e.Status) { case DownloadStatus.Paused: case DownloadStatus.Error: case DownloadStatus.Timeout: foreach (DBMyDownloadInfo downInfo in DownFactory.GetAllRecords()) { if (downInfo.VID == downloader.Tag as string) { if ((DateTime.Now.Second - _lastWriteDB.Second) >= _minDBOperationInterval) { DownFactory.UpdateRecord(downloader.Tag as string, (int)e.Status, downloader.BytesReceived); _lastWriteDB = DateTime.Now; } break; } } info.StatusImage = "/PPTVForWP7;component/Images/pause.png"; break; case DownloadStatus.Downloading: info.StatusImage = "/PPTVForWP7;component/Images/Download.png"; break; case DownloadStatus.Completed: MyDownloaded.Add(info); MyDownloading.Remove(info); break; case DownloadStatus.Waiting: info.StatusImage = "/PPTVForWP7;component/Images/waiting.png"; break; case DownloadStatus.InsufficientDiskSpace: MessageBox.Show("空间不足,请删除一些下载任务", "提示", MessageBoxButton.OK); break; default: break; } break; } } DownFactory.UpdateRecord(downloader.Tag as string, (int)e.Status, -1); }
private void xAction_Tap(object sender, System.Windows.Input.GestureEventArgs e) { Grid grid = sender as Grid; MediaFileDownload downloader = MediaFileDownload.Find(grid.Tag as string); if (downloader != null) { switch (downloader.Status) { case DownloadStatus.Downloading: case DownloadStatus.Waiting: downloader.Pause(); break; case DownloadStatus.Error: case DownloadStatus.Paused: case DownloadStatus.Timeout: case DownloadStatus.InsufficientDiskSpace: { _vid = downloader.Tag as string; foreach (DownInfo info in MyDownloading) { if (info.VID == _vid) { info.StatusImage = "/PPTVForWP7;component/Images/Download.png"; break; } } string[] id = _vid.Split('|'); if (string.IsNullOrEmpty(id[1])) { //电影 StartDownloadProgram(Convert.ToInt32(id[0])); } else { //剧集 StartDownloadProgram(Convert.ToInt32(id[2])); } } break; default: break; } } }
public static void UpdateTask(string vid, string downUrl) { MediaFileDownload.Find(vid).MediaFileUrl = new Uri(downUrl, UriKind.RelativeOrAbsolute); string fileName = MediaFileDownload.Find(vid).MediaFileUrl.AbsolutePath; if (fileName[0] == '/') { fileName = fileName.Remove(0, 1); } MediaFileDownload.Find(vid).FileName = fileName; DownFactory.UpdateFileNameRecord(vid, MediaFileDownload.Find(vid).PathName); foreach (DownInfo info in MyDownloading) { if (info.VID == vid) { info.LocalPathName = MediaFileDownload.Find(vid).PathName; break; } } MediaFileDownload.Find(vid).Start(); }
/// <summary> /// Lanza el evento de descarga de una entrada de un blog /// </summary> private void RaiseEvent(MediaFileModel file, int actual, int total) { MediaFileDownload?.Invoke(this, new EventArguments.MediaFileDownloadEventArgs(file, actual, total)); }
public static void BuildDownInfos() { if (DownFactory == null) DownFactory = new DBMyDownloadFactory(); if (MyDownloading == null) MyDownloading = new ObservableCollection<DownInfo>(); if (MyDownloaded == null) MyDownloaded = new ObservableCollection<DownInfo>(); ObservableCollection<DBMyDownloadInfo> downRecords = DownFactory.GetAllRecords(); MyDownloading.Clear(); MyDownloaded.Clear(); foreach (DBMyDownloadInfo record in downRecords) { string progressText = String.Format("正在下载:{0}M/{1}M", ((double)record.Progress / (double)(1024 * 1024)).ToString("f2"), ((double)record.FileLen / (double)(1024 * 1024)).ToString("f2")); double progressNumeric = 0; if (record.FileLen == 0) progressNumeric = 0; else progressNumeric = (double)record.Progress / (double)record.FileLen * 100; string subTitle = string.Empty; string[] id = record.VID.Split('|'); if (!string.IsNullOrEmpty(id[1])) { subTitle = "(第" + (Convert.ToInt32(id[1]) + 1).ToString() + "集)"; } MediaFileDownload downloader = new MediaFileDownload(); downloader.MediaFileUrl = new Uri(record.FileUrl, UriKind.RelativeOrAbsolute); downloader.Tag = record.VID; downloader.PathName = record.LocalPathName; downloader.DownloadProgressChange += OnDownloadProgressChange; downloader.DownloadStatusChange += OnDownloadStatusChange; string statusImage = string.Empty; if (record.Status != (int)DownloadStatus.Completed) { Utils.MediaFileDownload.Add(downloader); switch (Utils.MediaFileDownload.Find(record.VID).Status) { case DownloadStatus.Paused: case DownloadStatus.Error: case DownloadStatus.Timeout: statusImage = "/PPTVForWP7;component/Images/pause.png"; break; case DownloadStatus.Downloading: statusImage = "/PPTVForWP7;component/Images/Download.png"; break; case DownloadStatus.Waiting: statusImage = "/PPTVForWP7;component/Images/waiting.png"; break; } } DownInfo info = new DownInfo(record.VID, record.TitleImgUrl, progressText, progressNumeric, record.Title, subTitle, statusImage, record.Rate,record.LocalPathName); if (record.Status != (int)DownloadStatus.Completed) { MyDownloading.Add(info); } else { MyDownloaded.Add(info); } } if (System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists("/PersonalCenter/MyDownload")) { string[] files = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().GetFileNames("/PersonalCenter/MyDownload/*.*"); foreach (string file in files) { if (!DownFactory.IsExistFile(file)) { System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().DeleteFile( System.IO.Path.Combine("/PersonalCenter/MyDownload", file)); } } } }
public static bool AddTask(ChannelDetailInfo channelDetailInfo, ProgramInfo programInfo) { MediaFileDownload downloader = new MediaFileDownload(); string index = (programInfo != null) ? programInfo.Index.ToString() : string.Empty; string channelVid = (programInfo != null) ? programInfo.ChannelVID.ToString() : string.Empty; downloader.Tag = channelDetailInfo.VID.ToString() + "|" + index + "|" + channelVid; if (DownFactory.IsExistVid(downloader.Tag as string)) return false; Utils.MediaFileDownload.Add(downloader); int rate = (int)channelDetailInfo.Mark; string subTitle = string.Empty; if (channelDetailInfo.TypeID != 1 && programInfo != null) { subTitle = "(第" + (programInfo.Index + 1).ToString() + "集)"; } DownFactory.InsertRecord(downloader.Tag as string, channelDetailInfo.ImgUrl, channelDetailInfo.Title, "", downloader.PathName, 0, 0, (int)DownloadStatus.Downloading, rate); DownInfo info = new DownInfo(downloader.Tag as string, channelDetailInfo.ImgUrl, "正在下载:0M", 0, channelDetailInfo.Title, subTitle, "/PPTVForWP7;component/Images/Pause.png", rate,String.Empty); MyDownloading.Add(info); foreach (MediaFileDownload down in Utils.MediaFileDownload.DownloadQueue) { down.DownloadStatusChange += OnDownloadStatusChange; down.DownloadProgressChange += OnDownloadProgressChange; } return true; }
public static void BuildDownInfos() { if (DownFactory == null) { DownFactory = new DBMyDownloadFactory(); } if (MyDownloading == null) { MyDownloading = new ObservableCollection <DownInfo>(); } if (MyDownloaded == null) { MyDownloaded = new ObservableCollection <DownInfo>(); } ObservableCollection <DBMyDownloadInfo> downRecords = DownFactory.GetAllRecords(); MyDownloading.Clear(); MyDownloaded.Clear(); foreach (DBMyDownloadInfo record in downRecords) { string progressText = String.Format("正在下载:{0}M/{1}M", ((double)record.Progress / (double)(1024 * 1024)).ToString("f2"), ((double)record.FileLen / (double)(1024 * 1024)).ToString("f2")); double progressNumeric = 0; if (record.FileLen == 0) { progressNumeric = 0; } else { progressNumeric = (double)record.Progress / (double)record.FileLen * 100; } string subTitle = string.Empty; string[] id = record.VID.Split('|'); if (!string.IsNullOrEmpty(id[1])) { subTitle = "(第" + (Convert.ToInt32(id[1]) + 1).ToString() + "集)"; } MediaFileDownload downloader = new MediaFileDownload(); downloader.MediaFileUrl = new Uri(record.FileUrl, UriKind.RelativeOrAbsolute); downloader.Tag = record.VID; downloader.PathName = record.LocalPathName; downloader.DownloadProgressChange += OnDownloadProgressChange; downloader.DownloadStatusChange += OnDownloadStatusChange; string statusImage = string.Empty; if (record.Status != (int)DownloadStatus.Completed) { Utils.MediaFileDownload.Add(downloader); switch (Utils.MediaFileDownload.Find(record.VID).Status) { case DownloadStatus.Paused: case DownloadStatus.Error: case DownloadStatus.Timeout: statusImage = "/PPTVForWP7;component/Images/pause.png"; break; case DownloadStatus.Downloading: statusImage = "/PPTVForWP7;component/Images/Download.png"; break; case DownloadStatus.Waiting: statusImage = "/PPTVForWP7;component/Images/waiting.png"; break; } } DownInfo info = new DownInfo(record.VID, record.TitleImgUrl, progressText, progressNumeric, record.Title, subTitle, statusImage, record.Rate, record.LocalPathName); if (record.Status != (int)DownloadStatus.Completed) { MyDownloading.Add(info); } else { MyDownloaded.Add(info); } } if (System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists("/PersonalCenter/MyDownload")) { string[] files = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().GetFileNames("/PersonalCenter/MyDownload/*.*"); foreach (string file in files) { if (!DownFactory.IsExistFile(file)) { System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().DeleteFile( System.IO.Path.Combine("/PersonalCenter/MyDownload", file)); } } } }