2013-11-26 50 views
0

我已經將導入按鈕與媒體元素鏈接起來,所以我可以播放歌曲。使用標籤和滑塊鏈接WPF上的媒體元素

  // Create OpenFileDialog 
     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); 



     // Set filter for file extension and default file extension 
     dlg.DefaultExt = ".txt"; 
     dlg.Filter = "WAV Files (*.wav)|*.wav|MP3 Files (*.mp3)|*.mp3|MP4 Files (*.mp4)|*.mp4|WMA Files (*.wma)|*.wma|SWA (*.swa)|*.swa"; 


     // Display OpenFileDialog by calling ShowDialog method 
     Nullable<bool> result = dlg.ShowDialog(); 


     // Get the selected file name and display in a TextBox 
     if (result == true) 
     { 
      // Open document 
      meMedia1.Source = new Uri(dlg.FileName); 
      meMedia1.Play(); 
      //txtFileLocation.Text = filename; 

現在,播放聲音,但我想要做的是鏈接一個滑塊,使他們能夠跳過一些歌曲,也滑塊上方的標籤,以便它讀取它有多長成的歌曲。這是我的應用程序現在看起來如何給你一個想法。

http://i.stack.imgur.com/sVtrd.png

謝謝。

編輯:得到了改變歌曲位置的想法,但我仍然無法得到它手動移動到歌曲的時間,例如,如果我跳到歌曲的中間,讓歌曲完成我的滑塊仍然會在中間,我希望它在最後。

回答

0

一種方法是創建一個DispatcherTimer,每隔200-800ms(取決於您的更新速度的偏好)進行打勾,以將滑塊同步到玩家的當前位置。該代碼可能看起來與此類似:

// In the class members area 
private DispatcherTimer _timer = null; 

// In your constructor/loaded method 
_timer = new DispatcherTimer(); 
_timer.Interval = TimeSpan.FromMilliseconds(500); 
_timer.Tick += _timer_tick; 

// Timer's tick method 
void _timer_tick(object sender, EventArgs e) 
{ 
    // Convert duration to an integer percentage based on current position of 
    // playback and update the slider control 
    TimeSpan ts = meMedia1.NaturalDuration.TimeSpan; 
    int percent = int(meMedia1.Position/ts.Seconds * 100); 
    mySliderControl.Value = percent; 
} 

注意,這裏假設你有一個Slider其最小爲0,最大爲100。您會碰到它到0-1000(並相應地改變數學),以獲得更精細的粒度。這也不允許滑塊將用戶交互推送回播放器,但是讓您瞭解一種相反的方式。您可以向滑塊添加事件處理程序,以便當用戶開始交互時,停止此_timer_timer.Stop()),以便更新媒體位置停止更新滑塊,而是開始執行滑塊 - >媒體位置更新。然後,當用戶放開滑塊時,打開_timer_timer.Start())。