简体   繁体   English

在WPF中播放mp3

[英]Playing mp3 in WPF

MediaElement doesnt work for me in my WPF application. MediaElement在我的WPF应用程序中对我不起作用。

mediaElement1.LoadedBehavior = MediaState.Manual;
mediaElement1.Source = new Uri(@"C:\Music\MySong.mp3", UriKind.RelativeOrAbsolute);
mediaElement1.Play();

When I do this in my Window1.xaml.cs file. 当我在我的Window1.xaml.cs文件中执行此操作时。 Nothing happens. 什么都没发生。 Atleast I cant hear anything. 至少我听不到任何声音。 I have tried all kind of different things, but no sound. 我尝试了各种不同的东西,但没有声音。

In winforms: 在winforms中:

axWindowsMediaPlayer1.URL = @"C:\Music\MySong.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();

Works without any problems. 工作没有任何问题。 Any simple solution or things to try? 任何简单的解决方案或尝试的事情?

Ok I solved it. 好的,我解决了。 WPF only support MediaElement if you have Windows Media Player 10 or above. 如果您使用的是Windows Media Player 10或更高版本,WPF仅支持MediaElement。 I was running WMP9. 我在运行WMP9。

Though I'm also new in wpf,One thing you should notice about media element is that providing source in the XAML tag is not worth working. 虽然我也是wpf的新手,但你应该注意媒体元素的一件事是在XAML标签中提供源代码不值得工作。 you need to provide the source with the urikind like this 你需要像这样提供urikind的源代码

media.Source = new Uri(@"E:\Pehli_Baar_Mohabbat.mp3",UriKind.RelativeOrAbsolute);

put this line in window constructor 把这一行放在窗口构造函数中

and set loadedbehavious=manual and then check. 并设置loadedbehavious=manual然后检查。

mediaElement1.LoadedBehavior = MediaState.Manual; mediaElement1.LoadedBehavior = MediaState.Manual; ---- edit to----- mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual; ----编辑到----- mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM