C#编写此代码更加简单 [英] C# writing this code a little more simpler

查看:58
本文介绍了C#编写此代码更加简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
 private void BeatDetector_Move(object sender, EventArgs e)
 {
     if (panel1.Contains(label))
     {
         if (BeatDetector.Location.X == label.Location.X)
         {


             if (label.Text == "KICK2")
             {
            SoundPlayer player = new SoundPlayer(Properties.Resources.KICK2);
            player.Play();
             }



         }
     }
 }



现在,在该程序中,我将分配更多的音频文件,例如100个或更多,我想知道是否有更简单的方法来编写此代码?因为我将不得不使用不同类型的音频文件一遍又一遍地重复这部分代码



Now in this program I will be adding allot more audio files like 100 or more, and I was wondering if there was a simpler way o write this code? Because I will have to repeat this part of the code over and over again with different type audio file

if (label.Text == "KICK2")
          {
         SoundPlayer player = new SoundPlayer(Properties.Resources.KICK2);
         player.Play();
          }

推荐答案

您可以使用^ ]来存储像{"KICK2", Properties.Resources.KICK2} 这样的对,并使用一个调用.请参阅我链接的Dictionary 文档页面上的代码示例.
You may use a Dictionary[^] for storing the pairs like {"KICK2", Properties.Resources.KICK2} and use a single call. See the code sample at the Dictionary documentation page I linked.


我认为SoundPlayer也是api函数
i think SoundPlayer is too api function
PlaySound("explosion.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);


但是您可以使用功能强大的工具,例如wmp.dll ...
您可以将声音添加到播放列表并播放声音文件...
如果您想简单地使用此..您可以使用url属性..


but you can use powerfull tools like wmp.dll...
you can add sound to a play list and play aound file...
if you wana simple to use this .. you can use url property..


这篇关于C#编写此代码更加简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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