如何在单击按钮时保持音乐播放 [英] How to keep music playing when button is clicked
本文介绍了如何在单击按钮时保持音乐播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发了一个简单的FlipFlop游戏应用程序。我在表单上有16个按钮,我想添加音乐文件作为背景音乐,但只要我点击任何按钮,音乐就会停止。我这样做了:
MediaPlayer mp = new MediaPlayer();
mp.Open(新的Uri(@101.Shahin&Sepehr - Tears Of Fire.mp3, UriKind.Relative));
mp.Play();
解决方案
查看 BackgroundWorker类 [ ^ ]。
播放音乐在DoWork事件中。
在按钮点击事件上设置下面的代码,你必须只设置wav文件。
< pre lang =cs> protected void btnclick_Click( object sender,EventArgs e)
{
string path = Server.MapPath( 歌曲/ welcome.wav跨度>);
playSound(path);
// System.Threading.Thread.Sleep(2000);
}
private void playSound( string path)
{
System.Media.SoundPlayer player =
new System.Media.SoundPlayer();
player.SoundLocation = path;
player.Load();
player.Play();
}
I have developed a simple application of FlipFlop game. I have 16 buttons on the form and i want to add music file as background music but as soon as i click any button, music stops. I did this:
MediaPlayer mp = new MediaPlayer();
mp.Open(new Uri(@"101. Shahin & Sepehr - Tears Of Fire.mp3", UriKind.Relative));
mp.Play();
解决方案
Look into the BackgroundWorker Class[^].
Play the music in the DoWork event.
Set below code on your button click event and you have to set only wav file.
protected void btnclick_Click(object sender, EventArgs e) { string path = Server.MapPath("Songs/welcome.wav"); playSound(path); //System.Threading.Thread.Sleep(2000); } private void playSound(string path) { System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.SoundLocation = path; player.Load(); player.Play(); }
这篇关于如何在单击按钮时保持音乐播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文