同时播放两种声音 [英] Play two sounds simultaneusly

查看:49
本文介绍了同时播放两种声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法同时播放两种声音?

Is there a way to play two sounds at the same time?

我知道 SoundPlayer 无法做到这一点.我不能使用 SoundEffect 因为我相信它只是 XNA 的一部分.

I know that SoundPlayer isn't able to do this. I can't use SoundEffect as I believe it's only part of XNA.

这两个所需的声音将在未知和随机时间被调用.播放后需要对声音进行控制.即,声音必须能够在播放完毕之前停止.

The two required sounds will be called at unknown and random times. The sound needs to be be controlled after it is played. i.e., the sound must be able to be stopped before it has finished playing.

推荐答案

参考 PresentationCoreWindowsBase 并试试这个...

Reference PresentationCore and WindowsBase and try this...

var p1 = new System.Windows.Media.MediaPlayer();
p1.Open(new System.Uri(@"C:windowsmedia	ada.wav"));
p1.Play();

// this sleep is here just so you can distinguish the two sounds playing simultaneously
System.Threading.Thread.Sleep(500);

var p2 = new System.Windows.Media.MediaPlayer();
p2.Open(new System.Uri(@"C:windowsmedia	ada.wav"));
p2.Play();

编辑我收到了反对票,可能是因为乍一看,它似乎会在第一个声音完成后播放第二个声音.不是,它们是由 Windows 异步播放的.睡眠就在那里,所以如果您逐字测试此代码,您可以听到声音一起播放,没有延迟就不会引起注意,因为它们是相同的声音.

EDIT I received a downvote probably because at first glance this looks like it will play the second sound after the first is finished. It doesn't, they are played by windows asynchronously. The sleep is there so if you test this code verbatim you can hear the sounds play together, it wouldn't be noticeable without the delay since they are the same sound.

这段代码演示了两个声音在彼此之上的单独线程上播放,这有点毫无意义,因为无论如何播放都不会阻塞

This code demonstrates the two sounds playing on separate threads on top of each other, which is sort of pointless since the playback doesn't block anyway

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"C:windowsmedia	ada.wav"));
        c.Play();
    }).Start();

System.Threading.Thread.Sleep(500);

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"C:windowsmedia	ada.wav"));
        c.Play();
    }).Start();

http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.stop.aspx该类还有停止播放所需的控件

http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.stop.aspx The class also has the control you need to stop playback

这篇关于同时播放两种声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆