2017-10-04 4 views
0

MediaElementを使用してビデオを再生しています。今、私はそれを再生する前に合計の時間を取得したい。どのように可能ですか?UWPのMediaElementを使用してビデオの合計時間を取得する方法

FileOpenPicker openPicker = new FileOpenPicker(); 
foreach (string extension in FileExtensions.Video) 
{ 
    openPicker.FileTypeFilter.Add(extension); 
} 
StorageFile file = await openPicker.PickSingleFileAsync(); 
// mediaPlayer is a MediaElement defined in XAML 
if (file != null) 
{ 
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 
    videoMediaElement.SetSource(stream, file.ContentType); 

    var totalDurationTime = videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds;//get value zero 
    var totalDurationTime1 = TimeSpan.FromSeconds(videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds);//get zero 
    videoMediaElement.Play(); 
} 
+1

MediaOpenedイベントを待ってから、ファイルがロードされた後に長さを確認する必要があるかもしれません。少なくともライブストリームの場合、[0]以外の値は、[ここ](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.mediaelement#Windows_UI_Xaml_Controls_MediaElement_MediaOpened)によると、設定したので、ローカルファイルの場合、そのイベントの後に長さを設定する必要があります – Hannes

答えて

1

として@HannesあなたはMediaElementクラスのNaturalDurationプロパティによってメディアのデュレーションを取得したい場合、あなたは例えば、MediaOpenedイベントハンドルの内側に上記のコードを配置する必要があり、言った:

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

実際には、ファイルVideoPropertiesでビデオファイルの長さを取得できます。ファイルを開く前にも継続時間を取得できます。 xaml.csで、その後

StorageFile file = await openPicker.PickSingleFileAsync(); 
Windows.Storage.FileProperties.VideoProperties videoProperties = await file.Properties.GetVideoPropertiesAsync(); 
Duration videoDuration = videoProperties.Duration; 
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"/> 

次のコードを使用して合計時間を取得ウィル

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

関連する問題