对 C# 非常陌生,我发现我有一个奇怪的代码问题。我有 C# 2010 的 Express 版本。我需要一个 WAV 文件在特定时间播放,例如上午 10 点、上午 1130 点和下午 2 点。我可以使用按钮让 WAV 播放,但在任何特定时间都不能在不单击按钮的情况下播放。有什么想法或建议吗?我一直在尝试使用 Timer 事件,但是当使用它时,甚至按钮都不起作用。
问问题
1950 次
2 回答
1
您需要使用计时器。让我们将计时器的间隔设置为 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();
}
于 2012-08-16T14:29:24.123 回答
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;
}
}
}
}
于 2012-08-16T14:04:30.570 回答