0

I am making a Windows 8.1 speech based application. My problem is that when I give Cortana the input it launches my app and the app closes at the splashscreen, but when I run my app in background (minimize the app) or when the app is running, the Cortana input works perfect.

Where am I going wrong? Here is my app.xaml.cs code, in the OnActivatedMethod:

if (args.Kind == ActivationKind.VoiceCommand)
        {
            VoiceCommandActivatedEventArgs vcArgs = (VoiceCommandActivatedEventArgs)args;

            string voiceCommandName = vcArgs.Result.RulePath.First(); // What command launched the app?

            switch (voiceCommandName) // Run the action specific to the command
            {
                case "comand1": // User said comand1
                    rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
                    break;

                case "comand2": // User said comand2
                    rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
                    break;
                case "comand3": // User said comand3
                    rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
                    break;
                case "comand4":
                    rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
                    break;
            }

and in the OnNavigated method of the speech handling page:

SpeechRecognitionResult vcArgs = e.Parameter as SpeechRecognitionResult;
        RcvdCommand = vcArgs.Text.ToUpper();

        // Check for null!
        string commandMode = vcArgs.SemanticInterpretation.Properties["commandMode"][0];

        if (commandMode == "voice") // Did the user speak or type the command?
        {
            RequestHanding();
            MyTextblock.Text = vcArgs.Text;
            // SpeakText(audioPlayer, String.Format(" app heard you say {0}", RcvdCommand ));

            // HandleNlpCommand(vcArgs);
        }
        else if (commandMode == "text")
        {
            // messageTextBox.Text = string.Format("Working on your request \"{0}\"", RcvdCommand);

            // HandleNlpCommand(vcArgs);
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71

1 Answers1

4

When the application is not running, and is activated by voice command, the OnLaunched() method is not called. So you need to call the code that ensures the root frame is created in the OnActivated() method as well:

  Frame rootFrame = Window.Current.Content as Frame;

  // Do not repeat app initialization when the Window already has content,
  // just ensure that the window is active
  if (rootFrame == null)
  {
    // Create a Frame to act as the navigation context and navigate to the first page
    rootFrame = new Frame();

    // ... restore app state, etc.

    Window.Current.Content = rootFrame;
  }
Jogy
  • 2,465
  • 1
  • 14
  • 9
  • 1
    Hey, thanks for the answer. It works. I just have one query, my app works and now it takes me to the page where it handles the cortana request, the only issue is it doesn't respond to the user via voice. It's supposed to talkback to the user with a reply. instead it does nothing. Also in an another scenario the app must talk back and then show the phonecallUI, instead it shows the PhoneCallUI directly without talk back. where am I going wrong? – iam.Carrot Dec 13 '15 at 09:22
  • How are you trying to make the app talk back to the user? I use this code: SpeechSynthesizer synthesizer = new SpeechSynthesizer(); var stream = await synthesizer.SynthesizeTextToStreamAsync(text); media.SetSource(stream, ""); where media is a MediaElement control on the page – Jogy Dec 13 '15 at 18:53
  • 1
    Yes, yes, I am using the same code. Also the whole process of talking back works perfectly fine when I use the app is running in the background, just when I launch the app using voice commands, the page gets navigated to the SpeechHandling page(thanks to you) but the app doesn't talk back to the user. – iam.Carrot Dec 15 '15 at 05:39
  • On a side Note. pleas make sure your App ony talks back when the user talks to it if the command is typed into Cortana the App should only reply with a UI. – DevEnitly Dec 24 '15 at 03:05