2013-08-05 33 views
0

我試圖做一個媒體播放器,你可以選擇來呈現來自URL或本地磁盤文件本地磁盤文件。我沒有問題,使得它打開並呈現URL文件Silverlight中的MediaElement,選擇

void LoadVideo_Click(object sender, RoutedEventArgs e) 
     { 
      LoadVideo.IsEnabled = false; 
      mediaElement.Source = new Uri(path, UriKind.Absolute); 

隨着string path = "http://www.blablabla.com/movie.wmv"

當我試圖指定本地磁盤文件的路徑(如「C:\ movie.wmv」出現該問題或@ 「C:\ movie.wmv」)。它根本不會那樣工作。

至於我看過了,你不必直接訪問文件,除了那些已經在項目目錄在硬盤上。我想要做的是:

  • 使用對話框,選擇要打開的文件
  • 保存文件的路徑爲字符串,將其傳送到MediaElement.Source

不幸的是,我不沒有線索怎麼做。我會很感激任何意見。

回答

1

在這裏你去,這應該做的伎倆:

 OpenFileDialog fdlg = new OpenFileDialog(); //you need to use the OpenFileDialog, otherwise Silverlight will throw a tantrum ;) 
     fdlg.Filter = "MP4 Files|*.mp4|AVI files|*.avi"; //set a file selection filter 

     if (fdlg.ShowDialog() != true) //ShowDialog returns a bool? to indicate if the user clicked OK after picking a file 
      return; 

     var stream = fdlg.File.OpenRead(); //get the file stream 
     //Media is a MediaElement object in XAML 
     Media.SetSource(stream); //bread and butter 

     Media.Play(); //no idea what this does 

Here's關於如何使用OpenFileDialog廣泛的例子。至於MediaElement,你可以在代碼中看到上面你所需要的全部是SetSource()方法(而不是在Source屬性)。

+0

非常感謝!使用SetSource方法非常簡單。歡呼我pozdrawiam;) – mas