WPF MediaElement播放方向

时间:2019-02-01 17:34:57

标签: wpf mp4 mediaelement

我有一些mp4(捕获在android上,有的是纵向的,有的是横向的) 当我将它们用作MediaElement标签的来源时,它们始终以横向模式播放。我一直在疯狂地搜索,并且在MediaElement中根本没有找到关于视频方向的任何信息,所以希望我错过了一些基本知识。

这是我的xaml:

<Canvas x:Name="videoCanvas" Height="Auto" Width="641" Margin="10,0,11,0" HorizontalAlignment="Center" >
    <ContentControl Content="{Binding Media}" Width="{Binding ActualWidth, ElementName=videoCanvas}"  Height="{Binding ActualHeight, ElementName=videoCanvas}" />
</Canvas>

及其背后的代码:

  media = new MediaElement();
  media.LoadedBehavior = MediaState.Manual;            
  media.Loaded += Media_Loaded;
  media.MediaOpened += Media_MediaOpened;
  media.Source = new Uri(@"c:\videos\portrait.mp4");
  media.HorizontalAlignment = HorizontalAlignment.Center;
  media.VerticalAlignment = VerticalAlignment.Center;

两个视频的Media_MediaOpened中的NaturalVideoHeight / Width都相同,所以我认为我不能用它来旋转MediaElement。

1 个答案:

答案 0 :(得分:0)

有一种方法可以解决我的问题。您需要使用掘金Microsoft-WindowsAPICodePack-Core和Microsoft-WindowsAPICodePack-Shell:

ShellFile shell = ShellFile.FromFilePath(this.VideoFile);
ShellProperty<ulong?> fs = shell.Properties.System.Size;

// workaround to find out if video in portrait or landscape
BitmapSource bs = shell.Thumbnail.BitmapSource;
double bsWidth = bs.Width;
double bsHeight = bs.Height;
videoOrientation = bsWidth > bsHeight ? VideoOrientation.LANDSCAPE : VideoOrientation.PORTRAIT;

ShellProperty<uint?> fw = shell.Properties.System.Video.FrameWidth;
ShellProperty<uint?> fh = shell.Properties.System.Video.FrameHeight;

从代码中,我们看到视频具有缩略图表示,它是您在Windows资源管理器中浏览视频时看到的图像。 使用“ fw”和“ fh”,您可以设置AspectRatio并正确设置MediaElement的Width和Height属性。

希望对您有所帮助。