2012-05-16 77 views
2

我使用MediaTimelineMediaClock來控制WMV視頻播放。獲得無視頻播放時間?

Timeline = new MediaTimeline(new Uri(VideoFileName)); 
Clock = Timeline.CreateClock(true) as MediaClock; 

當我看Clock.NaturalDuration,值設置爲Automatic,並且不包含 時間TimeSpan呢。

當我分配到ClockMediaElement視頻開始播放,我現在可以看到NaturalDuration.TimeSpan存在並確定。

有沒有辦法讓視頻持續時間,而不是分配時鐘媒體元素和播放?

即使沒有使用媒體元素(這將是最好的),是否有辦法獲得持續時間?

回答

5

我有類似的需要檢查視頻長度,所以我讀到,當MediaOpened被引發的事件此屬性是可用的,否則被設置爲自動 - 閱讀本 http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.naturalduration(v=vs.95).aspx

media.MediaOpened += new System.Windows.RoutedEventHandler(media_MediaOpened); 
      media.LoadedBehavior = MediaState.Manual; 
      media.UnloadedBehavior = MediaState.Manual; 
      media.Play(); 

void media_MediaOpened(object sender, System.Windows.RoutedEventArgs e) 
     { 
      progress.Maximum = (int)media.NaturalDuration.TimeSpan.TotalSeconds; 
      timer.Start(); 
      isPlaying = true; 
     } 
0

檢查HasTimeSpan財產以確保它的存在

private void Element_MediaOpened1(object sender, RoutedEventArgs e) 
     { 
      if (mediaElement_1.NaturalDuration.HasTimeSpan) 
       timelineSlider.Maximum = mediaElement_1.NaturalDuration.TimeSpan.TotalSeconds; 
     } 
0

獲得視頻文件的持續時間在Win保留時間應用或地鐵C#

string path = ApplicationData.Current.LocalFolder.Path; 
StorageFile videoFile = 
     await StorageFile.GetFileFromPathAsync(presentationItem.Slide_path_local); 
Windows.Storage.FileProperties.VideoProperties x = 
     await videoFile.Properties.GetVideoPropertiesAsync(); 
Duration videoDuration = x.Duration; 

獲得視頻文件持續時間的最佳方式