2012-09-25 37 views

回答

8

有兩個問題。首先,MediaElement只能在媒體加載後才能設置位置,通過處理MediaOpened事件來確定。其次,並非所有媒體都是可以追求的。請致電CanSeek查詢。使用類似:

playbackElement1.AutoPlay = true; 
// Will fire after the media has loaded 
playbackElement1.MediaOpened += (source, args) => 
{ 
    MediaElement player = (MediaElement) source; 
    if (player.CanSeek) 
    { 
     player.Position = new TimeSpan(0, 0, 0, 0, 5000); 
    } 
}      
playbackElement1.SetSource(stream, this.m_recordStorageFile.FileType); 

一旦加載,使用NaturalDuration屬性,看到媒體的長度,將其轉換爲如果需要使用HasTimeSpanTimeSpan性質的時間跨度。

0

簡而言之,你將需要添加的MediaElement下方的滑塊,就像在任何視頻播放器...... - 滑塊將更新玩家當前位置 - 定時器將更新滑塊每300毫秒與當前播放位置

代碼:

<Slider x:Name="videoSlider" Thumb.DragStarted="videoSlider_DragStarted" Thumb.DragCompleted="videoSlider_DragCompleted" Margin="10,0,10,30" Height="18" VerticalAlignment="Bottom"/> 

<MediaElement x:Name="videoPlayer" Margin="4,59,152,53" Volume=".5" MediaOpened="videoPlayer_MediaOpened"> 

[這是圖片爲媒介,元素與Slider] [1]

現在我們有一個媒體元素和一個滑塊...下一篇:C#代碼:)

  public partial class MainWindow : Window 
     { 
       bool isSeekingMedia = false; 
       DispatcherTimer seeker; // the timer to update the Slider 
       public MainWindow() 
       { 
        InitializeComponent(); 
        player = new MediaPLayer(); 
        IsPlaying(false); 
        seeker = new DispatcherTimer(); 
        seeker.Interval = TimeSpan.FromMilliseconds(200); 
        seeker.Tick += Seeker_Tick; 
     } 
    ///Seeker_Tick will update the Slider position while the video is playing 
       private void Seeker_Tick(object sender, EventArgs e) 
       { 
        try 
        { 
         MediatimeCounter.Content = String.Format("{0:hh}:{0:mm}:{0:ss}/{1:hh}:{1:mm}:{1:ss}", videoPlayer.Position, videoPlayer.NaturalDuration.TimeSpan); 
         if (!isSeekingMedia) 
         { 
          videoSlider.Value = videoPlayer.Position.TotalSeconds; 
         } 
        } 
        catch (Exception ex) { } 
       } 


//This code is going to set Seeking to true to avoid playing the video if the user is changing the slider position... it kinda causes a performance issue.. so it's better to update the video position once the slider dragging event is completed. 
private void videoSlider_DragStarted(object sender, RoutedEventArgs e) 
    { 
     isSeekingMedia = true; 
    } 

//and this code is to update the video position based on the slider value. 
private void videoSlider_DragCompleted(object sender, RoutedEventArgs e) 
    { 
     if (videoPlayer.Source != null) 
     { 
      isSeekingMedia = false; 
      this.videoPlayer = player.SeekVideo(videoPlayer, videoSlider.Value); 
     } 
    } 

enter image description here

相關問題