user3011301
user3011301

Reputation: 21

C# playing multiple audio files at once from your resources?

I'd like to be able to press a button to play a sound of a button being pressed while music is already playing. SoundPlayer is no help because it stops the music to play the sound. I have added the sounds to my Resources (WindowsFormsApplication2.Resources.Properties.button) and I don't want to have to type in the file location.

Upvotes: 2

Views: 2190

Answers (1)

hammus
hammus

Reputation: 2612

Answer from MerickOWA

You CANNOT play two sounds at once using SoundPlayer.

SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.

There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)

The simplest option would be to depend on windows media player to play the audio for you.

Add a reference to "C:\Windows\System32\wmp.dll"

then use

var player = new WMPLib.WindowsMediaPlayer();
player.URL = @"..\..\bin\debug\tribal dance.wav";

Note: play starts immediately after setting the URL property

The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.

Upvotes: 1

Related Questions