【问题标题】:Play WAV at certain time? [closed]在特定时间播放 WAV? [关闭]
【发布时间】:2012-08-16 13:57:06
【问题描述】:

对 C# 非常陌生,我发现我有一个奇怪的代码问题。我有 C# 2010 的 Express 版本。我需要一个 WAV 文件在特定时间播放,例如上午 10 点、上午 1130 点和下午 2 点。我可以使用按钮让 WAV 播放,但在任何特定时间都不能在不单击按钮的情况下播放。有什么想法或建议吗?我一直在尝试使用 Timer 事件,但是当使用它时,甚至按钮都不起作用。

【问题讨论】:

  • 你能告诉我们不工作的代码吗,也许在我们的帮助下你会找到解决方案。

标签: c# audio timer wav


【解决方案1】:

您需要使用计时器。让我们将计时器的 Interval 设置为 1 秒。然后在计时器滴答事件中检查当前系统时间。如果它与特定时间(11 AM / 11:30 AM / 2PM)匹配,则停止计时器并播放声音。声音播放结束后,再次启动计时器。

private void MyTimer_Tick(object sender, EventArgs e)
{
        DateTime todayNow = DateTime.Now;

        // For 11 AM
        if (todayNow.Equals(new DateTime(todayNow.Year, todayNow.Month, todayNow.Day, 11, 00, 0)))
        {
            MyTimer.Stop(); // Stop the timer before you play the wav file
            PlaySound();
        }
        // For 11 30 AM
        else if (todayNow.Equals(new DateTime(todayNow.Year, todayNow.Month, todayNow.Day, 11, 30, 0)))
        {
            MyTimer.Stop(); // Stop the timer before you play the wav file
            PlaySound();
        }
        // For 2 PM
        else if (todayNow.Equals(new DateTime(todayNow.Year, todayNow.Month, todayNow.Day, 14, 00, 0)))
        {
            MyTimer.Stop(); // Stop the timer before you play the wav file
            PlaySound();
        }
}


// Once the Sound playing is over you can start the timer immediately
void OnSoundPlayOver
{
   MyTimer.Start(); 
}

【讨论】:

    【解决方案2】:

    这是我制作的与您所要求的类似的代码。在其中,用户设置他们想要倒计时的秒数(滴答声),当它完成计数时,它会播放“YellowSubmarine”。所以,这不是基于时间的,但希望它能让你走上正轨:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Media;
    
    namespace TickCounter_MGilliland
    {
        public partial class Form1 : Form
        {
    
            int NumberOfTicks;
            SoundPlayer Song = new SoundPlayer("YellowSubCut.wav");
            bool AlarmGo = false;
    
    
            public Form1()
            {
                InitializeComponent();
    
                NumberOfTicks = 1;
    
                SecondTimer.Interval = 1000;
                SecondTimer.Enabled = true;
    
                Progress.Maximum = 100;
                Progress.Value = 0;
            }
    
            private void StartButton_Click(object sender, EventArgs e)
            {
                if (InputTicks.Text != string.Empty)
                {
                    try
                    {
                        // Get the number of ticks that the user wants and set the input to ""
                        NumberOfTicks = Int16.Parse(InputTicks.Text);
                        InputTicks.Text = string.Empty;
                    }
                    catch (Exception s)
                    {
                        MessageBox.Show("Exception: "+ s.ToString());
                        InputTicks.Text += " <-FixMe";
                    }
    
                    if (NumberOfTicks > 0)
                    {
                        // Set ShowTicks' text to the number of ticks and show it
                        ShowTicks.Text = NumberOfTicks.ToString();
                        ShowTicks.Show();
    
                        InputTicks.ReadOnly = true;
    
                        AlarmGo = true;
                        Progress.Value = Progress.Maximum = NumberOfTicks;
    
                        // Start the timer
                        SecondTimer.Start();
                    }
                    else
                        MessageBox.Show("Input Must be an unsigned number greater than 0!");
    
                }
                else
                    MessageBox.Show("I can't count ticks you haven't given, Sherlock!");
            }
    
            private void StopButton_Click(object sender, EventArgs e)
            {
                InputTicks.ReadOnly = false;
                SecondTimer.Stop();
                ShowTicks.Text = string.Empty;
                ShowTicks.Hide();
                Progress.Value = 0;
                Song.Stop();
                MessageBox.Show("Phew... I'm glad you stopped that...\nIt was really starting to tick me off.");
            }
    
            private void SecondTimer_Tick(object sender, EventArgs e)
            {
                if (NumberOfTicks > 0)
                {
                    // Decrease the number of ticks and change the value in ShowTicks
                    ShowTicks.Text = (--NumberOfTicks).ToString();
                    Progress.Value = NumberOfTicks;
                }
                else
                {
                    NumberOfTicks = 0;
                    SecondTimer.Stop();
                    if (AlarmGo)
                        Song.PlayLooping();
                    AlarmGo = false;
                }
            }
        }
    }
    

    【讨论】:

    • 感谢两位的回答。它现在像梦一样工作。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2012-09-29
    • 2021-09-25
    • 2016-05-05
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多