5

我有一个计时器应用程序,我想在计时器完成后振动手机。我目前可以播放声音,直到按下 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();
}

更新:我已经尝试过乔的回应,如果我不打电话MessageBox.Show(),它似乎会停止,直到按下 OK。

4

2 回答 2

2

VibrateController.Start(Timespan)如果您传递的值大于 5 秒,则会引发错误,因此您需要做一些技巧来保持它的运行。创建一个计时器,并将其设置为重新开始振动。例如:

已编辑

愚蠢的我,我忘记了 Messagebox 和 DispatcherTimer 都将在同一个线程上运行。消息框将阻止它。试试这个。

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 秒关闭一次,直到您调用Change()传入 Timeout.Infinite

需要注意的几点:

  • vibrate.Start(vibrateDuration)仅从滴答事件中调用那是因为计时器将立即启动。
  • 根据神秘人的建议,我添加了一个超时。你会想要这样的东西。
  • 您可能想要重构并清理我的示例
于 2011-12-22T17:46:10.733 回答
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));
           }
        }
}
于 2011-12-22T17:43:06.667 回答