将.mp4保存到Windows Phone视频库

本文关键字:Phone 视频 Windows mp4 保存 | 更新日期: 2023-09-27 18:24:58

我想知道如何从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);
            }
        }
    }
}

将.mp4保存到Windows Phone视频库

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

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();
}