0

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.

1

2 Answers 2

1

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

1
  • I tried but without any effect. Maybe I wrote a bad code ;) Does NAudio support playing Resource file? If yes, how can I do that? Sep 21, 2014 at 11:39
1

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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.