简体   繁体   中英

Play multiple sounds using SoundPlayer

I'm making a sampler program where each key from 1 to 9 will make a different sound. Everything works fine, but when I press two (or more) sounds at the same time, the second one "kills" the first one.

I'm playing the sounds from .WAV files, using SoundPlayer . How can I solve this?

您需要使用 DirectX (DirectSound) 或一些旨在允许同时播放多个声音的类似 API。

There is one simple way to play multiple sounds at once in C# or VB.Net. You will have to call the mciSendString() API Function to play each .wav file. You won't even have to do multi-threading, unless you are loop-playing. Here is a complete working example of a MusicPlayer class created using mciSendString() .

// Sound api functions
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);

In the above function, the key is first parameter command. As long as you call two functions with a separate command name, they will play separately/simultaneously. This is what I did in one of my C# programs:

private void PlayWorker()
{
    StringBuilder sb = new StringBuilder();
    mciSendString("open \"" + FileName + "\" alias " + this.TrackName, sb, 0, IntPtr.Zero);
    mciSendString("play " + this.TrackName, sb, 0, IntPtr.Zero);
    IsBeingPlayed = true;
}

EDIT: Added link to a working example.

You could do this:

SoundPlayer supports WAV Stream. You could

  • MIX samples you play 'by-hand' and,
  • Fake (get the WAV header from somewhere, it's not complicated).

And provide such stream as a parameter to the SoundPlayer constructor .

That way you won't have to use somehow complicated DirectSound libraries, and still have mixing (multiple sounds at once).

using System.Windows.Media

Function void Play(string audioPath)
{
MediaPlayer myPlayer = new MediaPlayer();
myPlayer.Open(new System.Uri(audioPath));
myPlayer.Play();
}

Play(Application.StartupPath + "\\Track1.wav");
Play(Application.StartupPath + "\\Track2.wav");

This code could play two audio files simultaneously, the call in second audio track2.wav will not disturb the play of track1.wav .

I'm guessing that in your KeyPress event (or whatever you're using) you're creating a new instance of SoundPlayer using the constructor that takes a path to the WAV file, and then calling its Play method. In theory this shouldn't cause the "mono" effect that you're encountering, since Windows has been capable of playing multiple WAV files simultaneously since Windows 98. What I think you're hearing (based on my own use of this class) is not a cutoff of the first sound when the second starts, but actually a glitch that results from overall playback pausing as the WAV file is loaded from disk.

Instead of loading up a new instance of SoundPlayer on each key press, try creating an array of class-scoped SoundPlayer objects and pre-loading them from disk in your form's Load event. Then just call each SoundPlayer's Play method when the key is pressed. This may fix your problem, although I think you will still get occasional glitches this way.

您需要通过 SharpDX 使用 xAudio2,它是专用于游戏开发的 DirectX 组件,您可以在此处找到完整的工作示例: https : //github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/XAudio2/PlaySound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM