经过一段时间的搜索,我发现了很多来源,但没有人可以帮助我...对不起,如果我错过了帖子那么。
我有一个UWP应用程序,应该在我说话时启动摄像头。
语音识别正常;一个启动相机的按钮也可以。但是,在语音事件回调失败时调用VisualStateManager.GoToState()
:/
我写了一个清理过的脚本示例(所有'使用'也被删除了):
MainPage.xaml中
<Page
x:Class="Alfred.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Alfred"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FaceProcessingState">
<VisualState x:Name="Stopped" />
<VisualState x:Name="Playing">
<VisualState.Setters>
<Setter Target="btnPlay.(UIElement.Visibility)" Value="Collapsed" />
<Setter Target="btnStop.(UIElement.Visibility)" Value="Visible" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CaptureElement x:Name="captureElement" Width="320" Height="240" Grid.ColumnSpan="3" Grid.RowSpan="3" />
<Viewbox Grid.Row="1" Grid.Column="1">
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<Button x:Name="btnPlay" Click="OnStart">
<SymbolIcon Symbol="Play" />
</Button>
<Button x:Name="btnStop" Click="OnStopAsync" Visibility="Collapsed">
<SymbolIcon Symbol="Stop" />
</Button>
</StackPanel>
</Viewbox>
</Grid>
</Page>
MainPage.xaml.cs中
namespace Test
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
StartSpeech();
}
async void OnStart(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Playing", false);
}
async void OnStopAsync(object sender, RoutedEventArgs e)
{
await this.cameraPreviewManager.StopPreviewAsync();
VisualStateManager.GoToState(this, "Stopped", false);
}
void OnStop(object sender, RoutedEventArgs e)
{
this.requestStopCancellationToken.Cancel();
}
private async void StartSpeech()
{
SpeechListener speechRecognition = new SpeechListener();
speechRecognition.Recognize += OnRecognize;
// [...] Start speech recognition [...]
}
private async void OnRecognize(object sender, SpeechContinuousRecognitionResultGeneratedEventArgs e)
{
if (e.Result.Confidence.ToString() == "Medium" || e.Result.Confidence.ToString() == "High")
{
VisualStateManager.GoToState(this, "Playing", false);
}
}
}
}
SpeechListener.cs
namespace Test {
class SpeechListener
{
public delegate void RecognizeEventHandler(object sender, SpeechContinuousRecognitionResultGeneratedEventArgs e);
public event RecognizeEventHandler Recognize;
public SpeechListener()
{
}
public async Task PrepareRecognizer()
{
recognizer = new SpeechRecognizer();
// [...]
recognizer.ContinuousRecognitionSession.ResultGenerated += Recognizer_ContinuousRecognitionSession_ResultGenerated;
// [...]
}
private void Recognizer_ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
Recognize?.Invoke(this, args);
}
}
}
如果我手动按下按钮,则会调用“OnStart”,并且“GoToState”执行得很好。
如果我说话,则会调用回调“OnRecognize”,并且对“GoToState”的调用会引发系统异常......
有任何线索吗?我不是真正的C#专家......
谢谢!
修改 @Nico Zhu answer指出了问题,我的具体案例问题在评论中得到解决;)
答案 0 :(得分:1)
语音识别正常;一个启动相机的按钮也可以。但是,在语音事件回调上调用VisualStateManager.GoToState()失败:/
由于语音识别器引发的事件发生在后台线程上,因此请创建对调度程序的引用以获取UI线程的更新。
if (e.Result.Confidence.ToString() == "Medium" || e.Result.Confidence.ToString() == "High")
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
VisualStateManager.GoToState(this, "Playing", false);
});
}
有关详情,请参阅Continuous dictation。