.net 如何从资源播放音频

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

How to play audio from resource

.netcompact-frameworkaudio

提问by user228502

I am trying to play audio from a resource using .NET Compact Framework. I added an audio file for the resource property in my application and am trying to use the below sample resource file reference code for...

我正在尝试使用 .NET Compact Framework 从资源播放音频。我在我的应用程序中为资源属性添加了一个音频文件,并尝试使用以下示例资源文件参考代码...

SoundPlayer player = new SoundPlayer(Assembly.GetExecutingAssembly().
    GetManifestResourceStream("blessedwav.wav"));
player.Play();

But this code doesn't play a WAV sound. How do I play the resource audio file using .NET Compact Framework 3.5?

但是此代码不会播放 WAV 声音。如何使用 .NET Compact Framework 3.5 播放资源音频文件?

回答by user228502

I got the solution. This code is working very well in .NET Compact Framework:

我得到了解决方案。此代码在 .NET Compact Framework 中运行良好:

// Convert a byte array to a stream
using (var audioStream = new MemoryStream(Properties.Resources.full_song_wav))
{
    using (var player = new SoundPlayer(audioStream))
    {
        player.Play()
    }
}

回答by Anuraj

Try this:

尝试这个:

//I added the file as a audio resource in my project
SoundPlayer player = new SoundPlayer(Properties.Resources.recycle);
player.Play();

I didn't try with .NET Compact Framework. But it is working for me in C#.

我没有尝试使用 .NET Compact Framework。但它在 C# 中对我有用。

回答by Dilan V

This should work for you:

这应该适合你:

Stream str = Properties.Resources.YourWaveSoundFile;
SoundPlayer plyr = new SoundPlayer(str);
plyr.Play();

Make sure you have using System.Mediaabove your namespace.

确保你有using System.Media你的命名空间。

回答by Se?kin Durgay

Resources.ResourceManager.GetStream("nudgeSound", Resources.Culture);
System.IO.Stream s = Resources.ResourceManager.GetStream("nudgeSound", Resources.Culture);
SoundPlayer player = new SoundPlayer(s);
player.Play();