2017-12-27 502 views
0

我正在嘗試將視頻導入UWP畫布。 我有這個代碼,成功導入圖片: 私人異步無效AddImageButton_Click(對象發件人,RoutedEventArgs e) { 圖片MyImage = new Image();UWP視頻導入

 var picker = new FileOpenPicker(); 
     picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
     picker.FileTypeFilter.Add(".jpg"); 
     picker.FileTypeFilter.Add(".png"); 

     StorageFile file = await picker.PickSingleFileAsync(); 

     if (file != null) 
     { 
      StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", file); 
      // var files = await file.GetFilesAsync(); 
      var bitmap = new BitmapImage(); 

      var stream = await file.OpenReadAsync(); 

      // AddHandler(, new ExceptionRoutedEventHandler(Bitmap_ImageFailed), true); 
      bitmap.ImageFailed += Bitmap_ImageFailed; 
      await bitmap.SetSourceAsync(stream); 
      MyImage.Source = bitmap; 

      AddHandler(ManipulationStartedEvent, new ManipulationStartedEventHandler(Image_ManipulationStarted), true); 
      AddHandler(ManipulationDeltaEvent, new ManipulationDeltaEventHandler(Image_ManipulationDelta), true); 
      AddHandler(ManipulationCompletedEvent, new ManipulationCompletedEventHandler(Image_ManipulationCompleted), true); 

      ManipulationMode = ManipulationModes.All; 
      MyImage.RenderTransform = ImageTransforms; 


      parentCanvas.Children.Add(MyImage); 
     } 

    } 

我試着調整它來導入視頻,但在將位圖轉換爲MediaPlayerElement時卡住了。有什麼建議麼?

謝謝!

回答

0

根據您上面添加圖片到Canvas的代碼,你可以試試下面的代碼添加MediaPlayerElement到畫布上,並使用FileOpenPicker到選擇器中的媒體文件作爲MediaPlayerElement的來源。您可以進行一些修改以滿足您的要求。

private async void AddMediaPlayerElementButton_Click_1(object sender, RoutedEventArgs e) 
{ 
    var picker = new FileOpenPicker(); 
    picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
    picker.FileTypeFilter.Add(".mp4"); 
    StorageFile file = await picker.PickSingleFileAsync(); 
    MediaPlayerElement mediaPlayer = new MediaPlayerElement() { AreTransportControlsEnabled = true }; 
    if (file != null) 
    { 
     mediaPlayer.Source = MediaSource.CreateFromStorageFile(file); 
    } 
    parentCanvas.Children.Add(mediaPlayer); 
} 
+0

這工作,非常感謝你! – kmash