Play multiple sounds using SoundPlayer

后端 未结 6 493
清酒与你
清酒与你 2020-11-27 22:12

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 seco

6条回答
  •  一生所求
    2020-11-27 23:00

    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.

提交回复
热议问题