public void loadLocalVideoData() { //加载正在下载视频数据 string loadingVideos = WpStorage.ReadIsolatedStorageFile(downingVideosIso); if (!string.IsNullOrEmpty(loadingVideos)) { DowningVideo = JsonConvert.DeserializeObject <ObservableCollection <DownVideoInfoViewMoel> >(loadingVideos); foreach (DownVideoInfoViewMoel video in DowningVideo) { DowningVideoids.Add(video.VideoId); } } else { DowningVideo = null; } //加载完成下载视频数据 string loadedVideos = WpStorage.ReadIsolatedStorageFile(downedVideosIso); if (!string.IsNullOrEmpty(loadedVideos)) { DownedVideo = JsonConvert.DeserializeObject <ObservableCollection <DownVideoInfoViewMoel> >(loadedVideos); foreach (DownVideoInfoViewMoel video in DownedVideo) { DownedVideoids.Add(video.VideoId); } } else { DownedVideo = null; } }
private void GetVideoData(IAsyncResult result) { #if DEBUG long memory = DeviceStatus.ApplicationCurrentMemoryUsage / (1024 * 1024); long memoryLimit = DeviceStatus.ApplicationMemoryUsageLimit / (1024 * 1024); long memoryMax = DeviceStatus.ApplicationPeakMemoryUsage / (1024 * 1024); System.Diagnostics.Debug.WriteLine("当前内存使用情况:" + memory.ToString() + " MB 当前最大内存使用情况: " + memoryMax.ToString() + "MB 当前可分配最大内存: " + memoryLimit.ToString() + " MB"); #endif try { WebResponse response = ((HttpWebRequest)result.AsyncState).EndGetResponse(result); Stream stream = response.GetResponseStream(); long totalValue = response.ContentLength; //以第一次记录的size为文件大小 if (currentDownVideo.TotalSize == 0) { currentDownVideo.TotalSize = totalValue; } else { totalValue = currentDownVideo.TotalSize; } byte[] data = new byte[32 * 1024]; //stream.Position = (long)currentDownVideo.Loadedsize; int read = 0; while ((read = stream.Read(data, 0, data.Length)) > 0) { if (CommonData.NetworkStatus != "WiFi") { StopGetVideoData(); break; } //if (streamToWriteTo.Length != 0 && totalValue > streamToWriteTo.Length) if (streamToWriteTo.Length != 0) { if (CallbackManager.currentPage != null) { CallbackManager.currentPage.Dispatcher.BeginInvoke(() => { try { if (streamToWriteTo.CanWrite) { currentDownVideo.LoadProgress = (double)streamToWriteTo.Length * 100 / (double)totalValue; currentDownVideo.Size = Convert.ToDouble((double)((totalValue) / (double)((double)1024 * (double)1024))).ToString("0.0") + "MB"; currentDownVideo.Loadsize = Convert.ToDouble((double)((streamToWriteTo.Length) / (double)((double)1024 * (double)1024))).ToString("0.0") + "MB"; currentDownVideo.Loadedsize = streamToWriteTo.Length; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("currentDownVideo异常:" + ex.Message); } }); } } streamToWriteTo.Write(data, 0, read); } //判断是否已经下载完成 if (totalValue <= streamToWriteTo.Length) { CallbackManager.currentPage.Dispatcher.BeginInvoke(() => { #if DEBUG memory = DeviceStatus.ApplicationCurrentMemoryUsage / (1024 * 1024); memoryLimit = DeviceStatus.ApplicationMemoryUsageLimit / (1024 * 1024); memoryMax = DeviceStatus.ApplicationPeakMemoryUsage / (1024 * 1024); System.Diagnostics.Debug.WriteLine("当前内存使用情况:" + memory.ToString() + " MB 当前最大内存使用情况: " + memoryMax.ToString() + "MB 当前可分配最大内存: " + memoryLimit.ToString() + " MB"); #endif currentDownVideo.IsLoading = false; currentDownVideo.IsLoaded = true; currentDownVideo.LocalDownloadUrl = CommonData.videoSavePath + currentDownVideo.VideoId.ToString() + ".mp4"; DownedVideo.Add(currentDownVideo); DownedVideoids.Add(currentDownVideo.VideoId); DowningVideo.RemoveAt(0); DowningVideoids.Remove(currentDownVideo.VideoId); SaveVideoData(); if (App.MainViewModel.NeedDownedTip) { App.ShowToast(currentDownVideo.name + " 下载完成"); } //进行下一个下载 isDownding = false; BeginDownVideos(); }); Debug.WriteLine("下载完成"); } else { CallbackManager.currentPage.Dispatcher.BeginInvoke(() => { currentDownVideo.IsLoadError = true; currentDownVideo.IsLoading = false; isDownding = false; //重新下载 BeginDownVideos(); }); } streamToWriteTo.Close(); } catch (System.Net.ProtocolViolationException e) { System.Diagnostics.Debug.WriteLine(e.Message); } catch (Exception e) { CallbackManager.currentPage.Dispatcher.BeginInvoke(() => { System.Diagnostics.Debug.WriteLine(e.Message); streamToWriteTo.Close(); currentDownVideo.IsLoadError = true; currentDownVideo.IsLoading = false; isDownding = false; //重新下载 BeginDownVideos(); }); } }