[英]How to play media element synchronously?
I have a datatemplate in which i have a text block and speech synthesizer. 我有一个数据模板,其中有一个文本块和语音合成器。 Whwn i bind it with data the template spawns atleast 3 children.
我将其与数据绑定在一起,模板产生了至少3个孩子。 A speech synthesizer is activated on click of one checkbox.
单击一个复选框即可激活语音合成器。 It works fine in normal conditions.
在正常情况下它可以正常工作。 But if i test it vigorously and try to play more than one synthesizer before initialization, it plays unexpected audio.
但是,如果我进行了严格的测试并尝试在初始化之前播放多个合成器,则它会播放意外的音频。 And it continue even after exiting from that page.
即使从该页面退出,它仍然继续。
I am sharing code for check box click event. 我正在共享复选框点击事件的代码。 Please suggest a solution.
请提出解决方案。
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
{
}
}
Have you tried using the MediaElement.CurrentState property? 您是否尝试过使用MediaElement.CurrentState属性?
MediaElement.CurrentState
here's a link http://msdn.microsoft.com/En-US/Library/Windows/Apps/windows.ui.xaml.controls.mediaelement.currentstate 这是链接http://msdn.microsoft.com/zh-CN/Library/Windows/Apps/windows.ui.xaml.controls.mediaelement.currentstate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.