如何同步播放媒体元素?

时间:2014-03-20 09:20:00

标签: c# wpf winrt-async

我有一个数据模板,其中有一个文本块和语音合成器。我用数据绑定它,模板产生了至少3个孩子。单击一个复选框即可激活语音合成器。它在正常条件下工作正常。但是,如果我在初始化之前大力测试并尝试播放多个合成器,它会播放意外的音频。即使退出该页面,它仍会继续。

我正在共享复选框点击事件的代码。请提出解决方案。

    private async void checkboxPlay_Click(object sender, RoutedEventArgs e)
    {
        // when _mediaCounter == 0, synthesizer is stopped or not played
        // when _mediaCounter == 1, it is playing
        // when _mediaVounter == 2, it is paused
        Grid gd = (Grid)((sender as CheckBox).Parent as Grid).Parent;
        var child = VisualTreeHelper.GetParent(gd);
        try
        {
            if (sender is CheckBox)
            {
                if (_listElement.Count > 0)
                {
                    if (sender != _listElement[_checkCounter].CheckBox && _listElement[_checkCounter].CheckBox != null)
                    {
                        _listElement[_checkCounter].MediaElement.Stop();
                        _mediaCounter = 0;
                        _timer.Stop();
                        _listElement[_checkCounter].Slider.Value = 0;
                        _description = string.Empty;
                        _listElement[_checkCounter].CheckBox.IsChecked = false;
                    }
                }
                CheckBox cb = sender as CheckBox;
                Grid x = (Grid)VisualTreeHelper.GetParent(cb);

                _mediaIndex = Convert.ToInt32(
                        x.DataContext.ToString().Substring(x.DataContext.ToString().Length - 1, 1)
                        ) - 1;
                _checkCounter = _mediaIndex;


                if (_description != cb.DataContext.ToString())
                {
                    cb.IsChecked = true;
                    _description = cb.DataContext.ToString();
                    _mediaCounter = 0;
                    _InitializeCheckbox(cb);
                    _InitializeMedia();
                }
            }

            if (_mediaCounter == 0)
            {
                _mediaCounter = 1;
                string desc = string.Empty;
                SpeechSynthesizer synth = new SpeechSynthesizer();
                SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(_description.ToString());
                _listElement[_checkCounter].MediaElement.SetSource(stream, stream.ContentType);
                _listElement[_checkCounter].MediaElement.Play();
            }
            else if (_mediaCounter == 1)
            {
                _listElement[_checkCounter].MediaElement.Pause();
                _timer.Stop();
                _mediaCounter = 2;
            }
            else
            {
                _listElement[_checkCounter].MediaElement.Play();
                _timer.Start();
                _mediaCounter = 1;
            }
        }
        catch
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用MediaElement.CurrentState属性?

MediaElement.CurrentState

这是一个链接 http://msdn.microsoft.com/En-US/Library/Windows/Apps/windows.ui.xaml.controls.mediaelement.currentstate