c#如何在倒计时期间的特定时间播放声音

时间:2015-10-06 23:46:54

标签: c# audio dispatchertimer

我一直在教自己c#并试图创建一个程序,到目前为止,我所研究和发现的一切都在工作,除了声音。程序从60分钟开始倒计时,当调度员达到15分钟,10分钟和5分钟剩余标记时,我希望每次都能播放声音。当我将声音链接到一个按钮时,它可以毫无问题地播放,但不会在任何一个间隔播放。这是我到目前为止的代码。

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    //timer
    private int time = 3600;
    private DispatcherTimer Timer;

    //Time intervals
    private const int MINUTE_15 = 900;
    private const int MINUTE_10 = 600;
    private const int MINUTE_5 = 300;

    //Warning intervals
    private SoundPlayer warningSound15 = new SoundPlayer(@"Resources\se15.wav");
    private SoundPlayer warningSound10 = new SoundPlayer(@"Resources\sse10.wav");
    private SoundPlayer warningSound5 = new SoundPlayer(@"Resources\se05.wav");

    public MainWindow()
    {
        InitializeComponent();

        Timer = new DispatcherTimer();
        Timer.Interval = new TimeSpan(0, 0, 0, 1);
        Timer.Tick += Timer_Tick;

    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (time > 0)
        {
            if (time <= 600)
            {
                if (time % 2 == 0)
                {
                    countdown.Foreground = Brushes.Red;
                }
                else
                {
                    countdown.Foreground = Brushes.DarkRed;
                }

                countdown.Text = string.Format("{0} minutes {1} seconds", time / 60, time % 60);
            }
            else
            {
                countdown.Text = string.Format("{0} minutes {1} seconds", time / 60, time % 60);
            }
        }
        else
        {
            Timer.Stop();
            hint.Clear();
            hint.Text += "BOOOOOOOOM!!!";
        }

        //Play warning sound  
        switch (time)
        {
            case MINUTE_15:
                warningSound15.Play();
                break;

            case MINUTE_10:
                warningSound10.Play();
                break;

            case MINUTE_5:
                warningSound5.Play();
                break;
        }
        time--;
    }


    //sounds

    private void hint_TextChanged(object sender, TextChangedEventArgs e)
    {
    }

    SoundPlayer hint_sound = new SoundPlayer(@"Resources\Ding.wav");

    private void button_Click(object sender, RoutedEventArgs e)
    {
        hint_sound.Play();
    }

    //hints

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "A straight flush has won this hand";
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "The blue trunk may require further examination";
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Does the diary entry provide any insight into the portraits?";
    }

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Now that you've assembled the pieces on the wall, do the colours look like anything?";
    }

    private void button5_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Perhaps the blacklight could be used on a lethal piece of evidence";
    }

    private void button7_Click(object sender, RoutedEventArgs e)
    {
        warningSound15.Play();
    }

    private void button8_Click(object sender, RoutedEventArgs e)
    {
        hint.Clear();
        hint.Text += "Good job!";
    }

    private void button14_Click_1(object sender, RoutedEventArgs e)
    {
        hint.Clear();
    }

    //timer control

    private void button6_Click_1(object sender, RoutedEventArgs e)
    {
        Timer.Stop();
    }

    private void button9_Click(object sender, RoutedEventArgs e)
    {
        Timer.Start();
    }

    private void button10_Click(object sender, RoutedEventArgs e)
    {
        Timer.Stop();
        time = 3600;
        countdown.Text = string.Empty;
        countdown.Text += "60 minutes 00 seconds";
        countdown.Foreground = Brushes.Black;
    }

    //music control

    private void button13_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement.Source != null)
            mediaElement.Play();
    }

    private void button12_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement.CanPause)
            mediaElement.Pause();
    }

    private void button11_Click(object sender, RoutedEventArgs e)
    {
        mediaElement.Position = TimeSpan.FromSeconds(0);
        mediaElement.Stop();
    }


}

}

0 个答案:

没有答案