使用MediaPlayer无法在VB.NET中播放MP3文件 [英] Trouble playing MP3 files in VB.NET with MediaPlayer
问题描述
我正在尝试播放MP3文件按钮单击事件。我有一个名为introduction.mp3的MP3文件,我已将其作为现有文件添加到我的项目中,并且我已经设置了它的属性
构建操作 - 内容
复制到输出目录 - 总是复制。
我的按钮点击事件中有以下代码,
Hi,
I am trying to play an MP3 file on a button click event. I hav ethe MP3 file named "introduction.mp3" which I have added as an existing file to my project, and I have set it's properties
Build Action - Content
Copy to Output Directory - Copy Always.
I then have the following code in my button click event,
mPlayer.Open(New Uri("Introduction.mp3", UriKind.Relative))
MessageBox.Show("The audio should be playing", "Playing", MessageBoxButton.OK)
mPlayer.Play()
mPlayer是我的MediaPlayer对象已经宣布在班级的顶部,
mPlayer is a MediaPlayer object that I have declared at the top of the class,
Dim mPlayer As New MediaPlayer
我检查过文件已复制到输出目录并播放当在widnows媒体播放器中运行时。
当我点击按钮时文件无法播放,任何人都可以告诉我我错过了什么,消息框确实显示所以我知道事件正在解雇,但没有声音。
有没有人有任何想法我在网上搜索了几个小时,我似乎找到的只是这个代码的例子这对我不起作用。
任何帮助都会很棒
谢谢
I have checked that the file has copied to the output directory and plays when run in widnows media player.
When I click the button the files does not play, can anyone tell me what I am missing, the message box does show so I know the event is firing but there is no sound.
Does anyone have any ideas I have been searching online for hours and all I seem to find is examples of this code which does not work for me.
Any help would be great
Thanks
推荐答案
我很确定你没有正确形成你的URI。使用消息框显示URI位置,我敢打赌它不是你的应用程序目录。
你可以这样做:
I'm pretty sure you are not forming your URI correctly. Use the message box to show the URI location and I would bet that its not your application directory.
You could do something like this:
Dim fileUri as Uri = New Uri(Application.StartupPath + @"\Introduction.mp3", UriKind.Absolute)
mPlayer.Open()
MessageBox.Show("The audio should be playing " & fileUri.ToString(), "Playing", MessageBoxButton.OK)
mPlayer.Play()
(您可能需要调整,我不是VB程序员)
您还应该将Build操作设置为None而不是Content。
(You may need to tweak, I'm not a VB programmer)
You should also set your Build action to "None" instead of Content.
如果你记得导入它确实会有所帮助你需要的一切,我错过了
It really does help if you remember to import everything you need, I had missed
imports System.Media
MediaPlayer似乎没有出现这样的错误。
MediaPlayer does not seem to throw errors for things like this.
这篇关于使用MediaPlayer无法在VB.NET中播放MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!