如何使您的应用程序在事后和事件中播放声音 [英] How to make your applicatiin play a sound in after and event

查看:81
本文介绍了如何使您的应用程序在事后和事件中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我很公平C#语言和程序设计的新手.我正在使用的程序是Visual Studio2015.我在一个简单的数学测验中完成了画龙点睛的工作,并遇到了问题.

I am fairly  new to the C# language and programming in general. The program I am using is Visual Studio 2015. I am putting the finishing touches on a simple math quiz and have encountered and issue.

教程的最后一步建议我在用户输入正确答案时使程序播放声音.它还指出,应该为每个控件 ValueChanged()事件编写一个事件处理程序.我不确定该写些什么 事件处理程序,以便在输入正确答案后使应用程序播放.谁能建议一种可以产生此功能的语法?如果您可以提供一种语法,可以帮助我在一般情况下向应用程序添加声音 使用C#语言,我也将不胜感激.

The last step of the tutorial suggest that I make the program play a sound when the user enters the correct answer. It also states that should write an event handler for each controls ValueChanged() event. I'm not sure what to write in the event handler in order to make the application play a when the correct answer is entered. Can anyone suggest a a syntax that will produce this functionality? If you can provide a syntax that will help me add sounds to application in general when using the C# Language I will appreciate that as well.

类似于本教程的步骤在这里:https://msdn.microsoft.com/en-us/library/dd492174.aspx

The like to the tutorial step is here: https://msdn.microsoft.com/en-us/library/dd492174.aspx

我的应用程序语法的相关部分如下:

The relevant portion of my application syntax is below:

private void timer1_Tick(对象发送者,EventArgs e)
       {
          如果(CheckTheAnswer())
           {
                             //如果CheckTheAnswer()返回true,则用户
                             //得到正确的答案.停止计时器
                             //并显示一个MessageBox.
                             timer1.Stop();
                             MessageBox.Show("您的答案正确无误!",
                    "恭喜!!);
                             startButton.Enabled = true;
           }
          否则,如果(timeLeft> 0)
           {
                             //如果CheckTheAnswer()返回false,请继续计数
                             //向下.将剩余时间减少一秒,然后
                             //通过更新
                             //剩余时间标签
                             timeLeft--;
                             timeLabel.Text = timeLeft +"seconds";
private void timer1_Tick(object sender, EventArgs e)
        {
            if (CheckTheAnswer())
            {
                //If CheckTheAnswer() returns true, then the user
                // get the answer right. Stop the timer
                // and show a MessageBox.
                timer1.Stop();
                MessageBox.Show("You got all the answers right!",
                    "Congratulations!");
                startButton.Enabled = true;
            }
            else if (timeLeft > 0)
            {
                // If CheckTheAnswer() return false, keep counting
                // down. Decrease the time left by one second and
                // display the new time left by updating the
                // Time Left Lable
                timeLeft--;
                timeLabel.Text = timeLeft + "seconds";

推荐答案

首先,您可以尝试直接在"if block"中直接播放声音.因此,在if(CheckAnswer())块中,您可以添加以下代码:

Well, first you can try simply playing a sound directly in the "if block". So in the if (CheckAnswer()) block you could add this:

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\windows\media\YouAreCorrect.wav");
player.Play();

在其他时间到了而他们还没有结束的地方,添加以下内容:

And in the else where the time is up and they didn't finish, add this:

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\windows\media\TimeIsUp.wav");
player.Play();

对于事件处理程序,它们可能的含义是为NumericUpDown的ValuedChanged事件添加一个事件.根据answer_Enter()处理函数的名称判断,假设您的UpDown控件名为"answer",该期望 发送者是一个NumericUpDown控件.因此,添加这样的新事件处理程序(如果尚未执行此操作):

As for the event handler, what they probably meant by that is to add an event for the NumericUpDown's ValuedChanged event. I'm assuming your UpDown control is named "answer", judging from the name of the answer_Enter() handler, which expects the sender to be a NumericUpDown control. So, add a new event handler like this (if you haven't already done this):

answer.ValueChanged += Sound_ValueChanged;


// and then, in the event handler, play a sound
private void Sound_ValueChanged(object sender, EventArgs e)
{
    // Generate sound when user enters correct answer
    NumericUpDown answerbox = sender as NumericUpDown;
    if (answerbox != true)
    {
        System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\windows\media\ValueChanged.wav");
        player.Play();     
    }
 }



这篇关于如何使您的应用程序在事后和事件中播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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