2011-12-22 56 views
5

我有一個計時器應用程序,我想振動手機,一旦計時器完成。我現在可以播放聲音,直到按下「確定」按鈕,但它只振動一次。如何重複振動,直到用戶按下OK按鈕?振動,直到消息框關閉Windows Phone 7

這是我當前的代碼

SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString); 

VibrateController vibrate = VibrateController.Default; 

vibrate.Start(new TimeSpan(0,0,0,0,1000)); 

MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK); 

if (alarmBox == MessageBoxResult.OK) 
{ 
    alarmSound.Stop(); 
    vibrate.Stop(); 
} 

更新:我已經嘗試了喬的反應,如果我不叫MessageBox.Show()看來,直到確定被壓在此時停止其工作。

+0

你想讓它持續振動,或脈衝? – Joe 2011-12-22 17:37:40

+0

@Joe我想讓它脈動 – Chris 2011-12-22 17:38:26

+6

您應該確保在一段時間後超時,否則您可能會冒電池電量供某人將手機放在另一個房間。 – 2011-12-22 17:45:28

回答

2

VibrateController.Start(Timespan)如果你傳遞一個值大於5秒就會拋出一個錯誤,所以你需要做一些掛羊頭賣狗肉,以保持它會。創建一個計時器,並將其設置爲重新啓動振動。例如:

編輯

我傻,我忘了這兩個消息框,並DispatcherTimer將在同一線程上運行。 Messagebox會阻止它。嘗試這個。

public partial class MainPage : PhoneApplicationPage 
{ 
    TimeSpan vibrateDuration = new TimeSpan(0, 0, 0, 0, 1000); 
    System.Threading.Timer timer; 
    VibrateController vibrate = VibrateController.Default; 
    int timerInterval = 1300; 
    SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString); 
    TimeSpan alramDuration; //used to make it timeout after a while 

    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     timer = new Timer(new TimerCallback(timer_Tick), null, 0, timerInterval); 
     alramDuration = TimeSpan.FromSeconds(0); 
     alarmSound.Play(); 
     MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK); 

     if (alarmBox == MessageBoxResult.OK) 
     { 
      StopAll(); 
     } 
    } 

    void timer_Tick(object sender) 
    { 
     //keep track of how long it has been running 
     //stop if it has gone too long 
     //otheriwse restart 

     alramDuration = alramDuration.Add(TimeSpan.FromMilliseconds(timerInterval)); 
     if (alramDuration.TotalMinutes > 1) 
      StopAll(); 
     else 
      vibrate.Start(vibrateDuration); 
    } 

    void StopAll() 
    { 
     timer.Change(Timeout.Infinite, Timeout.Infinite); 
     vibrate.Stop(); 
     alarmSound.Stop(); 
    } 
} 

所以我使用的是System.Threading.Timer,而不是調度。它主要是一樣的,只是少一點顯而易見的API。不要致電Start() and Stop(),您必須通過延遲金額。要啓動它,通過在0它會繼續下去,關每1.3秒,直到您叫Change()傳遞Timeout.Infinite

有幾件事情需要注意:

  • vibrate.Start(vibrateDuration)由蜱稱爲事件。這是因爲計時器會立即啓動。
  • Mystere Man's建議,我添加了一個超時。你會想要這樣的事情。
  • 您可能需要重構,並清理我的樣本
+0

我認爲MessageBox.Show()干擾了這一點。如果我刪除了時間和振動停止方法,那麼在按下確定按鈕後它會開始持續振動 – Chris 2011-12-22 18:00:54

+0

@Chris如果從if語句中刪除停止()? – Joe 2011-12-22 18:30:03

+0

如果我按下確定按鈕 – Chris 2011-12-22 19:46:18

0
SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString); 

VibrateController vibrate = VibrateController.Default; 

var vibrationLength = 1000; 
var startTime = DateTime.Now; 
vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength)); 

MessageBoxResult alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK); 
var ok = false; 

While (!ok){ 
    if (alarmBox == MessageBoxResult.OK) 
    { 
    ok = true; 
    } 
    else{ 
    if(startTime.AddMilliseconds(vibrationLength * 1.2) < DateTime.Now) 
    { 
     startTime = DateTimne.Now; 
     vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength)); 
    } 
    } 
} 

alarmSound.Stop(); 
vibrate.Stop(); 

大約

我不寫Windows Phone的代碼,所以這可能導致手機無法響應。但是一般的想法是不斷檢查,看看用戶是否已經打好了,如果沒有,如果沒有足夠的時間已經從最後一次振動開始新的振動。

如果你想要它脈衝,我會建議使用AddMilliseconds並添加一個長度大於你想要的脈衝長度。

使用定時器來代替

假裝你的類看起來像這樣

public class buzzz 
{ 

    MessageBoxResult alarmBox; 
    DispatchTimer alarmTimer = new DispatchTimer(); 
    var vibrationLength = 1000.0; 
    var timerIncrease = 1.2; 
    VibrateController vibrate = VibrateController.Default; 

    public buzz() 
    { 
     alarmTimer.Interval = TimeSpan.FromMillseconds(vibrationLegnth * timerIncrease); 
     alarmTimer.Tick += alarmTimer_Tick 
    } 
    public void startBuzz() 
    { 
     SoundEffectInstance alarmSound = PlaySound(@"Alarms/"+alarmSoundString); 
     vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength)); 
     alarmTimer.Start(); 
     alarmBox = MessageBox.Show("Press OK to stop alarm", "Timer Finished", MessageBoxButton.OK); 
    } 

    void alarmTimer_Tick(object sender, EventArgs e) 
     { 
      if(alarmBox == MessageBoxResult.OK) 
      { 
       alarmTimer.Stop(); 
       vibrate.Stop(); 
      } 
      else 
      { 
       vibrate.Start(new TimeSpan(0,0,0,0,vibrationLength)); 
      } 
     } 
}