0
votes

I have tried a couple of ways. One being the SoundPlayer, but to my understanding that can only play 1 sound at a time, and I need background music looping. I have also used the winmm.dll to try and play looping music, but it doesnt seem to work for me. Also, I cant seem to play the sound more than once. Here is what I have tried

    [DllImport("winmm.dll")]
    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);


        mciSendString(@"open C:\\Users\\jberry\\Documents\\Pics\\MusicLoop2.wav type waveaudio alias applause", null, 0, IntPtr.Zero);
        mciSendString(@"play applause", null, 0, IntPtr.Zero);

This works, but will only play once. I have seen a lot of post that say do something like this

        mciSendString(@"open C:\\Users\\jberry\\Documents\\Pics\\MusicLoop2.wav type waveaudio alias applause", null, 0, IntPtr.Zero);
        mciSendString(@"play applause repeat", null, 0, IntPtr.Zero);

But that wont even play anything. Could anyone help me find a quick way to play multiple sounds + a repeating song loop? If I didnt have to install any DLLs that would be nice, but I will do whats needed

1
I would highly suggest using NAudio. It's an open source library that can be easily installed via NuGet. No need to P/Invoke. It supports playing concurrent sounds. - Zer0
Just installed it, but I have no clue on how to use it. Looking up documentation, but is there any trick to it? - BlueBaroo
Googling "NAudio Tutorial" returned many useful results (and even two videos). Just follow a tutorial (and if you do end up using it close this question) - Scott Chamberlain
It's pretty easy to use. I'd look at the "how to" section here and they have included demos. But here's something specific to looping audio - Zer0
I have it looping now. Thank you. The other issue I have is that I am not able to play the sound a second time after playing it once - BlueBaroo

1 Answers

0
votes

When the original post mentions getting the sound to play once but there is no sound when trying to loop, there was really nothing wrong with the code. The problem is in the mciSendString - it could not loop a wav file. If you need to loop a sound, use an mp3 file.

Bonus tip: instead of using string commands to mciSendString, I use c#/vb.net wrapper provided by the article in this link.

Here is how I use the wrapper:

Dim myPlayer = New MciPlayer("resources\TimerAlert.mp3", "myAlias")
myPlayer.PlayLoop()
myPlayer.StopPlaying()