将.mp4保存到Windows Phone视频库

时间:2014-09-27 21:03:15

标签: c# download windows-phone-8.1 mp4 win-universal-app

我想知道如何从URI下载MP4视频文件并将其保存到Windows Phone 8.1上的媒体库中。 如果它在通用应用程序中运行会很棒 - 但它没有必要。

我发现这个代码将图像保存到相机胶卷 - 我是否用* .mp4以同样的方式将其保存到视频库?我可以将下载流(不确定是否有意义)交给该功能吗?

StorageFolder testFolder = await StorageFolder.GetFolderFromPathAsync(@"C:\test");
StorageFile sourceFile = await testFolder.GetFileAsync("TestImage.jpg");
StorageFile destinationFile = await KnownFolders.CameraRoll.CreateFileAsync("MyTestImage.jpg");

using (var sourceStream = await sourceFile.OpenReadAsync())
{
    using (var sourceInputStream = sourceStream.GetInputStreamAt(0))
    {
        using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (var destinationOutputStream = destinationStream.GetOutputStreamAt(0))
            {
                await RandomAccessStream.CopyAndCloseAsync(sourceInputStream, destinationStream);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

所以我终于明白了,这就是我的代码:

var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);

if (response.IsSuccessStatusCode)
{
    var file = await response.Content.ReadAsByteArrayAsync();
    StorageFile destinationFile 
        = await KnownFolders.SavedPictures.CreateFileAsync("file.mp4",
            CreationCollisionOption.ReplaceExisting);

    Windows.Storage.Streams.IRandomAccessStream stream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);
    IOutputStream output = stream.GetOutputStreamAt(0);

    DataWriter writer = new DataWriter(output);
    writer.WriteBytes(file);
    await writer.StoreAsync();
    await output.FlushAsync();
}