在Timer端播放声音文件

时间:2013-06-09 03:27:28

标签: windows-phone-8

我有一个倒数计时器,我想要一个警报在它到达终点时熄灭...很多适用于Android和iOS,但我找不到WP8。

我正在使用DispatcherTimer进行倒计时。

我正在寻找一些有关如何在计时器完成倒计时时调出声音的信息或示例。我点击按钮启动计数器并长按重置。这一切都很好,只是想在倒数后停止时产生声音效果。

    private DateTime EndTime { get; set; }
    private DispatcherTimer _dispatcherTimer;

    private void BtnCounter_Click(object sender, RoutedEventArgs e)
    {            

        if (this._dispatcherTimer == null)
        {
            this._dispatcherTimer = new DispatcherTimer();
            this._dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
            this._dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        }

        if (this.EndTime == DateTime.MinValue)
        {
            this.EndTime = DateTime.Now + (TimeSpan)this.tsPicker.Value;
        }

        this._dispatcherTimer.Start();


    }

    void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        var remaining = this.EndTime - DateTime.Now;
        int remainingSeconds = (int)remaining.TotalSeconds;
        this.tsPicker.Value = TimeSpan.FromSeconds(remainingSeconds);

        if (remaining.TotalSeconds <= 0)
        {
            this._dispatcherTimer.Stop();
            // Sound code should go here, or a method call to it.
        }
    }

    private void BtnCounter_Hold(object sender, System.Windows.Input.GestureEventArgs e)
    {
        cnt = 0;
        BtnCounter.Content = cnt;
        this._dispatcherTimer.Stop();
        this.EndTime = DateTime.MinValue;
        this.tsPicker.Value = TimeSpan.FromSeconds(0);
    }

    protected override void  OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        this.EndTime = DateTime.MinValue;
        base.OnNavigatedFrom(e);
    }

1 个答案:

答案 0 :(得分:1)

您是否尝试使用MediaElement?似乎有一个使用它的好例子,标题为Windows Phone 8: Playing Sounds