How to play audio from resource

吃可爱长大的小学妹 提交于 2019-11-28 08:12:59

问题


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...

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?


回答1:


I got the solution. This code is working very well in .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()
    }
}



回答2:


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#.




回答3:


This should work for you:

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

Make sure you have using System.Media above your namespace.




回答4:


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


来源:https://stackoverflow.com/questions/1900707/how-to-play-audio-from-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!