Esempio n. 1
0
        public void AddDownVideo(VideoInfo videoInfo)
        {
            if (videoInfo.downloadUrl != null && videoInfo.downloadUrl.Count == 0)
            {
                App.ShowToast(videoInfo.name + " 没有可用下载链接");
                return;
            }
            DownVideoInfoViewMoel video = new DownVideoInfoViewMoel();

            video.Name       = videoInfo.name;
            video.Image      = videoInfo.image;
            video.Time       = videoInfo.time;
            video.VideoId    = videoInfo.videoId;
            video.VideoIndex = videoInfo.videoIndex;
            video.Desc       = videoInfo.desc;
            video.IsLoading  = false;
            video.IsLoaded   = false;
            int downIndex = 0;

            for (int i = 0; i < videoInfo.downloadUrl.Count; i++)
            {
                if (videoInfo.downloadUrl[i].name == App.DownVideoModel.CurrentDefinitionName)
                {
                    downIndex = i;
                    break;
                }
            }
            video.DownUrl = videoInfo.downloadUrl[downIndex].url;
            DowningVideo.Add(video);
            DowningVideoids.Add(video.VideoId);
            SaveVideoData();
            BeginDownVideos();
        }
Esempio n. 2
0
        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();
                });
            }
        }