在 WPF 中将声音作为资源播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29552373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:04:18  来源:igfitidea点击:

Playing sounds in WPF as a resource

c#wpfaudioresources

提问by TypicalGC7

First of all, I've searched the site and looked at thisbut unfortunately it did not help me much.

首先,我搜索了该站点并查看了此内容,但不幸的是它对我没有太大帮助。

Whenever I click a frog image that I made, I want to play a .wav file as a resource. Here's my code:

每当我单击我制作的青蛙图像时,我都想播放 .wav 文件作为资源。这是我的代码:

void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"G:\COSR3S\FrogsAndLilyPads\FrogsAndLilyPads\Sounds\FrogCroak.wav");
    player.Play();
}

Of course this worked but my problem is the directory changes from computer to computer, so I thought I'd have to play it as a resource. I don't know how to do that. I am a fairly new programmer, just to keep in mind.

当然这行得通,但我的问题是目录在计算机之间发生变化,所以我想我必须将它作为资源播放。我不知道该怎么做。我是一个相当新的程序员,只是要记住。

I tried a few variations based on the aforementioned linkand played around with it. Examples:

我根据上述链接尝试了一些变体并进行了尝试。例子:

void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
    Uri uri = new Uri(@"pack://application:,,,/Sounds/FrogCroak.wav");
    var player = new MediaPlayer();
    player.Open(uri);
    player.Play();
}

I also tried doing this, although I thought this one wouldn't work, but I tried it anyway:

我也尝试过这样做,虽然我认为这行不通,但我还是尝试了:

void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
    Uri uri = new Uri(@"pack://application:,,,/Sounds/FrogCroak.wav");
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(uri);
    player.Play();
}

I was not sure if the "pack://application:,,," was something that would refer to the file being played.

我不确定“ pack://application:,,,”是否是指正在播放的文件。

I also tried to make it as if it was an image, like so:

我也试着让它好像是一个图像,像这样:

void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
    System.Media.SoundPlayer player = new System.Media.SoundPlayer(new Uri(@"pack://application:,,,/Sounds/FrogCroak.wav", UriKind.RelativeOrAbsolute));
    player.Play();
}

So instead of new BitmapImageI did new System.Media.SoundPlayerand only to find out that it wouldn't work that way.

所以,而不是new BitmapImage我做了new System.Media.SoundPlayer,只是发现它不会那样工作。

EDIT: I forgot to mention that this worked, but it did not play any sound.

编辑:我忘了提到这有效,但它没有播放任何声音。

void newFrog_MouseUp(object sender, MouseButtonEventArgs e)
{
    Uri uri = new Uri(@"pack://application:,,,/Sounds/FrogCroak.wav");
    var player = new MediaPlayer();
    player.Open(uri);
    player.Play();
}

Edit2: The possible duplicate technically didn't solve my problem, but according to the accepted answer it is not possible. Is there any other way I could play the audio using a different method?

Edit2:技术上可能的重复并没有解决我的问题,但根据接受的答案,这是不可能的。有没有其他方法可以使用不同的方法播放音频?

回答by David Dowdle

This works for me using Visual Studio 2013 with a command line application:

这对我使用带有命令行应用程序的 Visual Studio 2013 有效:

  • Embed the .wav file as a resource (Resources.resx) in the project
    • I used Add Resource from Existing file
    • Set the persistence for the file as 'Linked at compile time'
  • Reference the file from code using something like this. Timer is the namespace this code is from and NotificationSound is what I called the embedded sound:

    var notificationSound = new SoundPlayer(Timer.Properties.Resources.NotificationSound);
    notificationSound.PlaySync();
    
  • 将 .wav 文件作为资源(Resources.resx)嵌入到项目中
    • 我使用了从现有文件中添加资源
    • 将文件的持久性设置为“编译时链接”
  • 使用类似的代码从代码中引用文件。Timer 是此代码来自的命名空间,NotificationSound 是我所说的嵌入声音:

    var notificationSound = new SoundPlayer(Timer.Properties.Resources.NotificationSound);
    notificationSound.PlaySync();
    

This allowed me to play the sound from the application after moving the executable and renaming the original sound file (just to check that it was actually embedded).

这允许我在移动可执行文件并重命名原始声音文件后从应用程序播放声音(只是为了检查它是否实际嵌入)。