打2的声音一起在同一时间 [英] playing 2 sounds together at same time

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

问题描述

设计一个游戏,任何笔记本电脑: 我想打2的声音在同一时间!背景音乐和声音时,上的一个按钮pressing! 使用System.Media不会允许我打2的声音在一起。对于背景音乐,我用下面的:

designing a game for any laptop: i want to play 2 sounds at the same time! a background music and a sound when pressing on a button! using System.Media wont allow me to play 2 sounds together.. for background music, i used the following:

SoundPlayer sp = new SoundPlayer(@"..\..\bin\debug\tribal dance.wav");
sp.Play;

如果我打另一个声音播放时,previous人阻止! 我想背景音乐继续玩,直到窗体关闭!并在同一时间内,如果一个按钮被点击,另一个声音将被与播放的音乐!

if i played another SoundPlayer, the previous one stops! i want the background music to keep playing until the form is closed! and at the same time, if a button is clicked, another sound will be played with the music!

我也尝试添加了Windows媒体播放器的工具箱,并用它(让我2打2的声音在同一时间),它工作正常,但路径会给出一个错误,如果我把它(@... \斌\调试\部落dance.wav); 我需要把这种方式才能被任何一台电脑读取... 有什么建议?

i also tried added the windows media player to the toolbox and used it(allowing me 2 play 2 sounds at same time), it works fine but the path will give an error if i put it (@""....\bin\debug\tribal dance.wav"); and i need to put it that way in order to be read by any computer... any suggestions?

推荐答案

您不能播放两个声音同时使用声音播放。

You CANNOT play two sounds at once using SoundPlayer.

声音播放是使用原生WINAPI PlaySound 功能完成这对播放声音的同时不支持的任务。创建声音播放的多个实例也无济于事。

SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.

有很多选择其中大部分涉及实施了很多低级别的API来窗口的本地音频的的DirectSound (注意也不是C#和需要的互操作code很多)

There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)

最简单的办法是依赖于Windows媒体播放器来播放音频给你。

The simplest option would be to depend on windows media player to play the audio for you.

添加一个引用到C:\ WINDOWS \ SYSTEM32 \ wmp.dll文件

Add a reference to "C:\Windows\System32\wmp.dll"

然后用

var player = new WMPLib.WindowsMediaPlayer();
player.URL = @"..\..\bin\debug\tribal dance.wav";

注:播放设置URL属性后立即启动

Note: play starts immediately after setting the URL property

这种方法的缺点是你依赖的媒体播放器为您的应用程序能够正常工作。从正面看,你可以使用格式的媒体播放器支持的任何文件。

The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.

这篇关于打2的声音一起在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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