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

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

问题描述

有没有在同一时间播放两个声音的方式?

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

我知道声音播放是无法做到这一点。
我不能使用 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.

推荐答案

参考 presentationCore WindowsBase 并尝试这个...

Reference PresentationCore and WindowsBase and try this...

var p1 = new System.Windows.Media.MediaPlayer();
p1.Open(new System.Uri(@"C:\windows\media\tada.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:\windows\media\tada.wav"));
p2.Play();

修改
我收到可能是因为downvote乍一看,这看起来像第一次完成后,将播放第二个声音。它不,它们是由窗户异步播放。睡眠是存在的,所以如果你测试这个code逐字你可以听到的声音一起玩,就不会没有延迟是明显的,因为它们是相同的声音。

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.

这code演示了两种声音打在单独的线程上对方,这是有点毫无意义,因为播放不会阻止反正顶部

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:\windows\media\tada.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:\windows\media\tada.wav"));
        c.Play();
    }).Start();

<一个href=\"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
该类还有你需要停止播放控制

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

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

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