in FamilyNotes/Speech/SpeechManager.cs [145:228]
public async Task<bool> StartContinuousRecognition()
{
// Compiling a new grammar is potentially a high-latency operation,
// and it's easy for various threads to call this method concurrently,
// so use a sempahore to serialize access to this method. The semaphore
// allows only one thread at a time to execute this code path.
await Mutex.WaitAsync();
if (IsInRecognitionSession)
{
// End the previous speech recognition session.
await EndRecognitionSession();
}
#if VERBOSE_DEBUG
Debug.WriteLine(
"SpeechManager: Starting recognition session: {0}",
RecognitionMode );
#endif
try
{
// If no mic is available, notify the user and reset mode to default.
if (!await IsMicrophoneAvailable())
{
if (RecognitionMode == SpeechRecognitionMode.CommandPhrases ||
RecognitionMode == SpeechRecognitionMode.Dictation)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("Microphone is not available.");
messageDialog.Commands.Add(new UICommand("Go to settings...", async (command) =>
{
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone"));
}));
messageDialog.Commands.Add(new UICommand("Close", (command) => { }));
await messageDialog.ShowAsync();
}
RecognitionMode = SpeechRecognitionMode.Default;
Mutex.Release();
return false;
}
// Compile the grammar, based on the value of the RecognitionMode property.
await CompileGrammar();
// You can attach these event handlers only after the grammar is compiled.
SpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
SpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
// Start the recognition session.
await SpeechRecognizer.ContinuousRecognitionSession.StartAsync();
// Keep track of the the recognition session's state.
IsInRecognitionSession = true;
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Continuous recognition session started" );
#endif
}
catch (Exception ex)
{
Debug.WriteLine("SpeechManager: Failed to start continuous recognition session.");
var messageDialog = new Windows.UI.Popups.MessageDialog(
$"{ex.Message}",
"Failed to start continuous recognition session");
messageDialog.Commands.Add(new UICommand("Go to settings...", async (command) =>
{
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-speech"));
}));
messageDialog.Commands.Add(new UICommand("Close", (command) => { }));
await messageDialog.ShowAsync();
Mutex.Release();
return false;
}
finally
{
Mutex.Release();
}
return true;
}