2016-02-14 160 views
0

試圖製作一些mediaplayer應用程序。我把媒體放在wpf上,寫代碼打開媒體。但是,當我嘗試玩什麼也沒有發生......MediaElement不播放mp3

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args) 
{ 
    if (args.Files.Count > 0) 
    { 
     foreach (StorageFile file in args.Files) 
     { 
      if (playlist.Contains(file.Path)) return; 
      playlist.Add(file.Path); 
     } 
    } 
} 

private void PlayButton_OnClick(object sender, RoutedEventArgs e) 
{ 
    MyMedia.Source = new Uri(playlist[0], UriKind.RelativeOrAbsolute); 
    MyMedia.Play(); 
} 

檢查發現的MediaElement源不是空的,它具有路徑的權值。 嘗試重建那樣,仍然不起作用

private async void PlayButton_OnClick(object sender, RoutedEventArgs e) 
    { 
     var stream = await Playlist[0].OpenAsync(FileAccessMode.Read); 
     MyMedia.SetSource(stream, Playlist[0].ContentType); 
     MyMedia.Play(); 
    } 
+0

確保MediaElement已添加到應用程序,而不是在代碼中,但在ui中的某處。你可以隱藏它,但它應該在窗口後面的根網格中。 –

+0

請舉例子? –

+0

[如何從MediaElement庫中播放文件?]可能重複(http://stackoverflow.com/questions/19576394/how-to-play-file-from-library-by-mediaelement) –

回答

2

對不起是我的錯。

在我的XAML中,我忘記了MediaElement控件的AutoPlay屬性設置爲false

這解決了我的問題。

0

情侶,你需要注意的事情。

foreach (StorageFile file in args.Files) 
{ 
    if(playlist.Contains(file.Path)) 
    { 
     return;// Dont return use continue. You will probably skip rest of the files. 
    } 
    playlist.Add(file.Path); 
} 

添加手錶/快速查看new Uri(playlist[0], UriKind.RelativeOrAbsolute)是否指向正確的位置。從存儲文件

+0

我認爲它是正確的「D:\ a \ 3 Doors Down - Here Without You.mp3」如果「D:\」模擬SD卡 –

0

安裝程序源:

var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3"); 
var stream = await storageFile.OpenAsync(FileAccessMode.Read); 
mediaElement.SetSource(stream, storageFile.ContentType); 
mediaElement.Play(); 

我從這個答案得到它:How to play file from Library by MediaElement?

+0

仍然無法正常工作... –

+0

您能否爲我提供樣品? –

+0

private async void PlayButton_OnClick(object sender,RoutedEventArgs e) var stream = await Playlist [0] .OpenAsync(FileAccessMode.Read); MyMedia.SetSource(stream,Playlist [0] .ContentType); MyMedia.Play(); } –