2014-09-27 129 views
0

我想知道如何從URI下載MP4視頻文件並將其保存到Windows Phone 8.1上的媒體庫中。 如果它在通用應用程序中工作會很好 - 但它不一定。將.mp4保存到Windows Phone視頻庫

我發現這段代碼將圖像保存到相機膠捲上 - 我是否用同樣的方式將它保存到視頻庫中?* .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); 
      } 
     } 
    } 
} 

回答

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

您應該啓用** ** ID_CAP_MEDIALIB_PHOTO能力,** ** WMAppManifest.xml訪問「** SavedPictures **「相冊。 – 2015-06-16 12:24:20

+0

嗨,我試過這個Windows Phone 8.1,但無法下載視頻。你可以請幫助我..我有視頻網址下載.. – Arsal 2016-04-26 16:57:24

+0

嗨,你可以請更新爲什麼這不起作用 – 2016-05-13 07:50:55