示例#1
0
        private async Task WriteBytesToFileAsync(IStorageFile file, byte[] bytes)
        {
#if DEBUG
            System.Diagnostics.Debug.WriteLine("WRITE: " + file.Name);
#endif
            IStorageFile tmpFile = await this.GetTmpFile();

#if DEBUG
            System.Diagnostics.Debug.WriteLine("TEMP FILE: " + tmpFile.Name);
#endif

            using (IRandomAccessStream stream = await tmpFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                using (IOutputStream outputStream = stream.GetOutputStreamAt(0L))
                {
                    using (DataWriter writer = new DataWriter(outputStream))
                    {
                        writer.WriteBytes(bytes);
                        await writer.StoreAsync();

                        writer.DetachStream();
                        await outputStream.FlushAsync();

                        await tmpFile.MoveAndReplaceAsync(file);
                    }
                }
            }

            //// allows all file handles to be released immediately
            //// instead of a delay to the next garbage collection
            //writer = null;
            //outputStream = null;
            //stream = null;
            //tmpFile = null;
            //GC.Collect();
            //GC.WaitForPendingFinalizers();
        }
        private async Task HandleDownloadAsync(DownloadOperation download, bool start)
        {
            try
            {
                Debug.WriteLine("Running: " + download.Guid);

                // Store the download so we can pause/resume.
                activeDownloads.Add(download);

                Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress);
                if (start) // Start the download and attach a progress handler.
                {
                    await download.StartAsync().AsTask(cts.Token, progressCallback);
                }
                else // The download was already running when the application started, re-attach the progress handler.
                {
                    await download.AttachAsync().AsTask(cts.Token, progressCallback);
                }

                ResponseInformation response = download.GetResponseInformation();

                Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode));

                // Move the downloaded file from Temporary folder to the selected folder
                if (StorageFileDictionary.ContainsKey(download.ResultFile.Name))
                {
                    StorageFile file_selectedLocation = StorageFileDictionary[download.ResultFile.Name];

                    // convert item to mp3 first
                    if (IsMusicDictionary[download.ResultFile.Name])
                    {
                        Debug.WriteLine("Converting video to mp3: " + download.Guid);

                        // Others in [File]
                        string OriginalFileName           = download.ResultFile.Name;
                        AudioEncodingQuality audioQuality = AudioQualityDictionary[download.ResultFile.Name];
                        bool ism4a = IsisM4ADictionary[download.ResultFile.Name];

                        IStorageFile       result    = null;
                        VideoMP3Transcoder extractor = new VideoMP3Transcoder(OriginalFileName, download.ResultFile, audioQuality, ism4a);
                        result = await Task.Run(async() =>
                                                await extractor.ConvertToMP3()
                                                );

                        Debug.WriteLine("Result: " + result != null);

                        try
                        {
                            await result.MoveAndReplaceAsync(file_selectedLocation);
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.Message.ToString());
                        }

                        if (result != null)
                        {
                            Debug.WriteLine("Done converting to mp3: " + download.Guid);
                        }
                        else
                        {
                            Debug.WriteLine("Error converting to mp3: " + download.Guid);
                        }
                    }
                    else
                    {
                        await download.ResultFile.MoveAndReplaceAsync(StorageFileDictionary[download.ResultFile.Name]);
                    }
                }
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("Canceled: " + download.Guid);
            }
            catch (Exception ex)
            {
                if (!IsExceptionHandled("Execution error", ex, download))
                {
                    throw;
                }
            }
            finally
            {
                activeDownloads.Remove(download);

                // Event calling for status msg
                DownloadStatusMessageChanged?.Invoke("Download complete", download.ResultFile.Name);
            }
        }