对C#来说很新,我发现我有一个奇怪的代码问题。我有C#2010的Express版本。我需要在特定时间播放WAV文件,例如10AM,1130AM和2PM。我可以使用按钮来播放WAV,但不是在任何特定时间没有单击按钮。任何想法或建议?我一直在尝试使用Timer事件,但是当使用它时,甚至按钮都不起作用。
答案 0 :(得分:1)
您需要使用计时器。让我们将定时器的间隔设置为1秒。然后在计时器刻度事件检查当前系统时间。如果它与特定时间(上午11点/上午11点30分/下午2点)匹配,则停止计时器并播放声音。一旦播放完声音再次启动计时器。
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();
}
答案 1 :(得分:0)
这是我制作的代码与您提出的代码类似。用户在其中设置他们想要倒数的秒数(滴答滴答),当它完成计数时,它将播放“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;
}
}
}
}