我正在使用MediaElement播放视频。现在我想在播放之前得到它的总持续时间。这怎么可能?
.png
答案 0 :(得分:1)
正如@Hannes所说,如果你想通过MediaElement
类的<ListView Name="DataTable" ItemsSource="{Binding Path=people}">
<ListView.View>
<GridView>
<GridViewColumn Width="50px">
<!-- This is column for checkboxes but it don't should be in header -->
</GridViewColumn>
<GridViewColumn Header="ID"
DisplayMemberBinding="{Binding PersonID, UpdateSourceTrigger=PropertyChanged}">
</GridViewColumn>
<GridViewColumn Header="FullName"
DisplayMemberBinding="{Binding FullName, UpdateSourceTrigger=PropertyChanged}" />
<GridViewColumn Width="50px">
<!-- This is column for "Edit" buttons but it don't should be in header -->
</GridViewColumn>
<GridViewColumn Width="50px">
<!-- This is column for "Delete" buttons but it don't should be in header -->
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
属性获取媒体持续时间,你需要将上面的代码片段放在MediaOpened
事件句柄中,例如:
NaturalDuration
实际上,您可以通过文件VideoProperties
获取视频文件的持续时间。您甚至可以在打开文件之前获得持续时间。
<MediaElement x:Name="videoMediaElement" MediaOpened="videoMediaElement_MediaOpened"></MediaElement>
private void videoMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
var totalDurationTime = videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds;
var totalDurationTime1 = TimeSpan.FromSeconds(videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds);
}
答案 1 :(得分:0)
在xaml中使用
<TextBox x:Name="startTime" Width="20" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" BorderThickness="1" InputScope="Number" />
和
<TextBox x:Name="endTime" Width="20" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,630,135" BorderThickness="1" InputScope="Number"/>
然后在xaml.cs文件中
long x = Int64.Parse(startTime.Text);
long y = Int64.Parse(endTime.Text);
var clip = await MediaClip.CreateFromFileAsync(pickedFile);
clip.TrimTimeFromStart = new TimeSpan(x * 10000000);
clip.TrimTimeFromStart = new TimeSpan(y * 10000000);
composition = new MediaComposition();
composition.Clips.Add(clip);
mediaElement.Position = TimeSpan.Zero;
mediaStreamSource = composition.GeneratePreviewMediaStreamSource((int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight);
mediaElement.SetMediaStreamSource(mediaStreamSource);
您将使用以下代码获得总持续时间:clip.OriginalDuration.TotalSeconds