Jan Bońkowski
Jan Bońkowski

Reputation: 548

Play sound from resource file using c# wpf

I was wondering if it's possible to play sound file (wav, mp3) from a Resource File. I need to play two sounds - one for background music (async in a loop), second one for a random sound. I tried SoundPlayer, but BGM stops when I play a random sound (of course I use two separate SoundPlayers). So, I'm looking for a C# WPF Library which I can use to play two or more sounds in one time from Resources. I'd be grateful if somebody could provide an example. Thank You in advance.

Upvotes: 0

Views: 1290

Answers (2)

Jan Bońkowski
Jan Bońkowski

Reputation: 548

I solved my problem with this code:

    private NAudio.Wave.WaveFileReader wave = new NAudio.Wave.WaveFileReader(Properties.Resources.bgm);
    private NAudio.Wave.DirectSoundOut outs = null;
    public void PlayBGM()
    {
        if (outs == null)
        {
            outs = new NAudio.Wave.DirectSoundOut();
            outs.Init(new NAudio.Wave.WaveChannel32(wave));
        }
        outs.Play();
    }

Upvotes: 1

mrsrizan
mrsrizan

Reputation: 311

Have you tried using NAudio library? It is very easy and supports lots of functionalities.

Upvotes: 1

Related Questions