wpf 如何播放声音文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22745278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play a sound file?
提问by Brian J
I have found a solution on here to play a sound file in WPF which I extracted into a method and call this method in another method. But the when the PlaySound()is called the sound doesn't play. Does anyone have any insight as to why this is happening? Also the sound files are marked as content, but changing to type resource didn't remedy the problem either?
我在这里找到了一个解决方案,可以在 WPF 中播放声音文件,我将其提取到一个方法中并在另一个方法中调用这个方法。但是当PlaySound()被调用时,声音不播放。有没有人对为什么会发生这种情况有任何见解?声音文件也被标记为内容,但更改为类型资源也没有解决问题?
My method to play a sound:
我播放声音的方法:
private void PlaySound()
{
Uri uri = new Uri(@"pack://application:,,,/Sounds/jabSound.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
Then I call the method like this but its doesn't play the sound file, PlaySound();
然后我像这样调用该方法,但它不播放声音文件, PlaySound();
回答by Liam McInroy
You could also use a SoundPlayer
你也可以使用 SoundPlayer
SoundPlayer player = new SoundPlayer(path);
player.Load();
player.Play();
Pretty self explanatory.
不言自明。
BONUSHere's how to have it loop through asynchronously.
奖励这里是如何让它异步循环。
bool soundFinished = true;
if (soundFinished)
{
soundFinished = false;
Task.Factory.StartNew(() => { player.PlaySync(); soundFinished = true; });
}
Opens a task to play the sound, waits until the sound is finished, then knows it is finished and plays again.
打开一个任务来播放声音,等到声音结束,然后知道它已经结束并再次播放。
回答by Anatoliy Nikolaev
It turns out that MediaPlayerdoes not play the music files in embedded resources, quote from Matthew MacDonald book: Pro WPF 4.5 in C#. Chapter 26:
事实证明,MediaPlayer不播放嵌入资源中的音乐文件,引用自 Matthew MacDonald book: Pro WPF 4.5 in C#. Chapter 26:
You supply the location of your file as a
URI. Unfortunately, thisURIdoesn't use the application pack syntax, so it's not possible to embed an audio file and play it using theMediaPlayerclass. This limitation is because theMediaPlayerclass is built on functionality that's not native to WPF—instead, it's provided by a distinct, unmanaged component of the Windows Media Player.
您将文件的位置作为
URI. 不幸的是, thisURIdoesn't use the application pack syntax,因此不可能嵌入音频文件并使用MediaPlayer该类播放它。此限制是因为MediaPlayer该类基于非 WPF 的本机功能构建,而是由 Windows Media Player 的一个独特的非托管组件提供。
Therefore, try setting the local path to the your music file:
因此,尝试设置音乐文件的本地路径:
private void PlaySound()
{
var uri = new Uri(@"your_local_path", UriKind.RelativeOrAbsolute);
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
For workaround, see this link:
有关解决方法,请参阅此链接:
回答by har07
In addition to @Anatoly's answer, I would suggest to listen to MediaFailedevent to check for MediaPlayerfailure (such as file not found due to wrong path to your .wav file). MediaPlayerdoesn't throw exception if it fails to load the media file, it triggers MediaFailedevent instead.
除了@Anatoly 的回答之外,我还建议侦听MediaFailed事件以检查是否MediaPlayer失败(例如,由于 .wav 文件的路径错误而未找到文件)。MediaPlayer如果无法加载媒体文件,则不会抛出异常,而是触发MediaFailed事件。
And if you're trying to use relative uri, remember that it means relative to your executable file location. In development phase, it is usuallyinside bin\debugfolder. So path to your .wav file should be "../../Sounds/jabSound.wav".
如果您尝试使用相对 uri,请记住这意味着相对于您的可执行文件位置。在开发阶段,它通常在bin\debug文件夹内。所以你的 .wav 文件的路径应该是"../../Sounds/jabSound.wav".
Uri uri = new Uri("../../Sounds/jabSound.wav", UriKind.Relative);
var player = new MediaPlayer();
player.MediaFailed += (o, args) =>
{
//here you can get hint of what causes the failure
//from method parameter args
};
player.Open(uri);
player.Play();

