C# MediaElement没有';我不能播放mp3
正在尝试制作一些mediaplayer应用程序。我将mediaelement放在wpf上,编写代码来打开媒体。但是当我试着玩的时候,什么都没有发生C# MediaElement没有';我不能播放mp3,c#,windows-phone-8.1,C#,Windows Phone 8.1,正在尝试制作一些mediaplayer应用程序。我将mediaelement放在wpf上,编写代码来打开媒体。但是当我试着玩的时候,什么都没有发生 public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args) { if (args.Files.Count > 0) { foreach (StorageFile file in args.Files) {
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();
}
有几件事你需要注意
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);
}
添加一个Watch/Quickwatch,查看新Uri(播放列表[0],UriKind.RelativeOrAbsolute)是否指向正确的位置。需要注意的几件事
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);
}
添加一个Watch/Quickwatch,查看新Uri(播放列表[0],UriKind.RelativeOrAbsolute)是否指向正确的位置。从存储文件设置源:
var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3");
var stream = await storageFile.OpenAsync(FileAccessMode.Read);
mediaElement.SetSource(stream, storageFile.ContentType);
mediaElement.Play();
我从这个答案中得到了答案:从存储文件设置源:
var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3");
var stream = await storageFile.OpenAsync(FileAccessMode.Read);
mediaElement.SetSource(stream, storageFile.ContentType);
mediaElement.Play();
我从这个答案中得到了答案:对不起,这是我的错误 在我的XAML中,我忘记了
MediaElement
控件的AutoPlay
属性设置为false
这解决了我的问题。对不起,这是我的错误 在我的XAML中,我忘记了
MediaElement
控件的AutoPlay
属性设置为false
这就解决了我的问题。确保MediaElement已添加到应用程序中,而不是添加到代码中,而是添加到ui的某个位置。您可以将其隐藏,但它应该在窗口后的根网格中。请举个例子?确保MediaElement添加到应用程序中,而不是代码中,而是ui中的某个位置。你可以把它隐藏起来,但它应该在窗口后的根网格中。请举个例子?我认为它可能是正确的“D:\a\3 Doors Doors Doors Doors Doors Doors Doors Doors Doors Doors Doors–Here Without You.mp3”如果“D:\”模拟SDCard我认为它是正确的“D:\a\3 Doors Doors Doors Doors Doors Doors Doors Doors Doors Doors–Here模拟SDCard您能给我一个示例吗?私有异步void PlayButton_OnClick(对象发送方,routedEventTargets e){var stream=wait Playlist[0].OpenAsync(FileAccessMode.Read);MyMedia.SetSource(stream,Playlist[0].ContentType);MyMedia.Play();}您在哪里声明了媒体元素?您的意思是?当然可以。一切都很好。我看没什么问题。你能给我发个存档文件吗?你能给我一个例子吗?私有异步void PlayButton_OnClick(对象发送者,routedEventTargets e){var stream=wait Playlist[0].OpenAsync(FileAccessMode.Read);MyMedia.SetSource(stream,Playlist[0].ContentType);MyMedia.Play();}你在哪里声明了媒体元素?你的意思是什么?当然可以。一切都很好。我看没什么问题。你能寄给我一份档案吗?