我有一个计时器应用程序,我想在计时器完成后振动手机。我可以播放声音,直到按下OK按钮,但它只振动一次。在用户按下OK按钮之前,如何重复振动?
这是我目前的代码
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();
}
更新:我已经尝试了Joe的回复,如果我不打电话MessageBox.Show()
,它会起作用。在按下OK之前,它似乎停止了。
答案 0 :(得分:2)
VibrateController.Start(Timespan)
将抛出错误,因此您需要做一些技巧来保持它。创建一个计时器,并将其设置为重新启动振动。例如:
<强>编辑强>
愚蠢的我,我忘记了Messagebox和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而不是Dispatcher。它主要是相同的,只是少了一个明显的API。而不是调用Start() and Stop()
,你必须传递延迟金额。要开始它,传入0.它将每1.3秒继续关闭,直到你通过Timeout.Infinite Change()
传递
有几点需要注意:
vibrate.Start(vibrateDuration)
仅在答案 1 :(得分: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手机编写代码,因此这可能会导致手机无法响应。但是一般的想法是继续检查以确定用户是否已经好了,如果没有,如果自从上一次振动开始新的时间以来已经有足够的时间用完了。
如果你想要脉冲我会建议使用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));
}
}
}