2012-08-16 74 views
-1

C#非常新,我發現我有一個奇怪的代碼問題。我有C#2010 Express版本。我需要一個WAV文件在特定時間播放,例如10AM,1130AM和2PM。我可以讓WAV使用按鈕播放,但不會在任何時候沒有點擊按鈕。任何想法或建議?我一直在嘗試使用Timer事件,但是當使用該事件時,甚至連按鈕都不起作用。在某些時候播放WAV?

+0

你可以告訴我們代碼不工作,也許然後在我們的幫助下,你會得到解決方案。 – TimVK 2012-08-16 13:59:34

回答

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(); 
} 
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; 
      } 
     } 
    } 
} 
+0

謝謝你們的答案。現在它像夢一樣工作。我感謝幫助! – DaveB 2012-08-16 16:49:37