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));
}
}
}
你想讓它持續振動,或脈衝? – Joe 2011-12-22 17:37:40
@Joe我想讓它脈動 – Chris 2011-12-22 17:38:26
您應該確保在一段時間後超時,否則您可能會冒電池電量供某人將手機放在另一個房間。 – 2011-12-22 17:45:28