Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play audio from resource

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?

like image 796
user228502 Avatar asked Dec 14 '09 12:12

user228502


People also ask

How do I play an audio file?

Open File Manager and navigate to the folder where the audio file is located. Drag the audio file icon from File Manager and drop it on the Audio main window. The Selected file is opened. If Automatically play audio file on Open is selected in the Options-Play dialog box, the audio file starts playing.

How do I listen to a WAV file?

Since WAV is quite a popular format, almost all devices today support it using built-in media players. On Windows, the Windows Media Player is capable of playing WAV files. On MacOS, iTunes or QuickTime can play WAV files. On Linux, your basic ALSA system can play these files.

What folder are audio files stored in Android?

The audio files are stored in botht the res/raw directory and the asset directory.


3 Answers

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()
    }
}
like image 143
user228502 Avatar answered Oct 16 '22 13:10

user228502


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

like image 26
Anuraj Avatar answered Oct 16 '22 12:10

Anuraj


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.

like image 44
Dilan V Avatar answered Oct 16 '22 13:10

Dilan V