如何同步播放媒体元素

本文关键字:播放 媒体 元素 同步 何同步 | 更新日期: 2023-09-27 17:51:00

我有一个数据模板,其中我有一个文本块和语音合成器。当我绑定它与数据模板产生至少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
        {
        }
    }

如何同步播放媒体元素

您是否尝试过使用MediaElement。现状后财产吗?

MediaElement.CurrentState

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