在WinForm中播放多种声音+循环音乐



我已经尝试了几种方法。一个是SoundPlayer,但据我所知,一次只能播放1个声音,我需要背景音乐循环。我也使用过winmm.dll来尝试播放循环音乐,但它似乎对我不起作用。此外,我似乎不能多次播放这种声音。以下是我尝试过的

[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);

这是有效的,但只会播放一次。我看到很多帖子说做一些类似的事情

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

但这甚至不会起到任何作用。有人能帮我找到一种快速播放多种声音+重复歌曲循环的方法吗?如果我不必安装任何DLL,那将是很好的,但我会做需要的事情

当最初的帖子提到可以播放一次声音,但在尝试循环时没有声音时,代码确实没有问题。问题出在mciSendString中-它无法循环wav文件。如果你需要循环播放声音,可以使用mp3文件。

额外提示:我使用的不是mciSendString的字符串命令,而是本链接中文章提供的c#/vb.net包装器。

以下是我如何使用包装器:

Dim myPlayer = New MciPlayer("resourcesTimerAlert.mp3", "myAlias")
myPlayer.PlayLoop()
myPlayer.StopPlaying()

最新更新