0

I have a universal app the uses voice synthesis. Running under WP8.1, it works fine, but as soon as I try Win8.1 I start getting strange behaviour. The actual voice seems to speak once, however, on the second run (within the same app), the following code hangs:

string toSay = "hello";
System.Diagnostics.Debug.WriteLine("{0}: Speak {1}", DateTime.Now, toSay);
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    System.Diagnostics.Debug.WriteLine("{0}: After sythesizer instantiated", DateTime.Now);

    var voiceStream = await synth.SynthesizeTextToStreamAsync(toSay);

    System.Diagnostics.Debug.WriteLine("{0}: After voice stream", DateTime.Now);

The reason for the debug statements is that the code seems to have an uncertainty principle to it. That is, when I debug through it, the code executes and passes the SynthesizeTextToStreamAsync statement. However, when the breakpoits are removed, I only get the debug statement preceding it - never the one after.

The best I can deduce is that during the first run through something bad happens (it does claim to complete and actually speaks the first time), then it gets stuck and can't play any more. The full code looks similar to this:

string toSay = "hello";
System.Diagnostics.Debug.WriteLine("{0}: Speak {1}", DateTime.Now, toSay);
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    System.Diagnostics.Debug.WriteLine("{0}: After sythesizer instantiated", DateTime.Now);

    var voiceStream = await synth.SynthesizeTextToStreamAsync(toSay);

    System.Diagnostics.Debug.WriteLine("{0}: After voice stream", DateTime.Now);

    MediaElement mediaElement;

    mediaElement = rootControl.Children.FirstOrDefault(a => a as MediaElement != null) as MediaElement;
    if (mediaElement == null)
    {
        mediaElement = new MediaElement();

        rootControl.Children.Add(mediaElement);
    }

    mediaElement.SetSource(voiceStream, voiceStream.ContentType);
    mediaElement.Volume = 1;
    mediaElement.IsMuted = false;

    var tcs = new TaskCompletionSource<bool>();                
    mediaElement.MediaEnded += (o, e) => { tcs.TrySetResult(true); };
    mediaElement.MediaFailed += (o, e) => { tcs.TrySetResult(true); };

    mediaElement.Play();                

    await tcs.Task;
1
  • The odds that anybody else can repro the deadlock from such a small snippet are never very good. You'll need to provide a small repro project, post it to a file sharing service. Commented Feb 24, 2015 at 19:14

2 Answers 2

0

Okay - I think I managed to get this working... although I'm unsure why.

using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    var voiceStream = await synth.SynthesizeTextToStreamAsync(toSay);
    MediaElement mediaElement; 
    mediaElement = rootControl.Children.FirstOrDefault(a => a as MediaElement != null) as MediaElement;
    if (mediaElement == null)
    {
        mediaElement = new MediaElement();

        rootControl.Children.Add(mediaElement);
    }

    mediaElement.SetSource(voiceStream, voiceStream.ContentType);
    mediaElement.Volume = 1;
    mediaElement.IsMuted = false;

    var tcs = new TaskCompletionSource<bool>();                
    mediaElement.MediaEnded += (o, e) => { tcs.TrySetResult(true); };               

    mediaElement.Play();                

    await tcs.Task;

    // Removing the control seems to free up whatever is locking 
    rootControl.Children.Remove(mediaElement);

}
2
  • You probably just need to dispose voiceStream object after tcs task ends. Commented Mar 3, 2015 at 5:16
  • I tried this, in place of removing the media element and it still didn't work. However, I didn't realise it implemented IDisposable, and I have now added it to a stacked using statement - so the code it, at least, neater. Commented Mar 3, 2015 at 7:50
-3

I am not sure what program language you are using. However this may help. This is in C# so this could help lead you in the right direction.

namespace Alexis
{
    public partial class frmMain : Form
    {

        SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        SpeechSynthesizer Alexis = new SpeechSynthesizer();
        SpeechRecognitionEngine startlistening = new SpeechRecognitionEngine();
        DateTime timenow = DateTime.Now;
    }
  
  
  //other coding such as InitializeComponent and others.
  // 
  //
  //
  //
  
       private void frmMain_Load(object sender, EventArgs e)
        {

            _recognizer.SetInputToDefaultAudioDevice();
            _recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Default Commands.txt")))));
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Shell_SpeechRecognized);
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Social_SpeechRecognized);
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Web_SpeechRecognized);
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Default_SpeechRecognized);
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(AlarmClock_SpeechRecognized);
            _recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(AlarmAM))));
            _recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(AlarmPM))));
            _recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(_recognizer_SpeechDetected);
            _recognizer.RecognizeAsync(RecognizeMode.Multiple);

            startlistening.SetInputToDefaultAudioDevice();
            startlistening.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices("alexis"))));
            startlistening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(startlistening_SpeechRecognized);

          
          //other stuff here..... Then once you have this then you can generate a method then with your code as follows
          //
          //
          //
          
          
          
           private void Default_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
              {
                  int ranNum;
                  string speech = e.Result.Text;
                  switch (speech)
                  {
                      #region Greetings
                      case "hello":
                      case "hello alexis":
                          timenow = DateTime.Now;
                          if (timenow.Hour >= 5 && timenow.Hour < 12)
                          { Alexis.SpeakAsync("Goodmorning " + Settings.Default.User); }
                          if (timenow.Hour >= 12 && timenow.Hour < 18)
                          { Alexis.SpeakAsync("Good afternoon " + Settings.Default.User); }
                          if (timenow.Hour >= 18 && timenow.Hour < 24)
                          { Alexis.SpeakAsync("Good evening " + Settings.Default.User); }
                          if (timenow.Hour < 5)
                          { Alexis.SpeakAsync("Hello " + Settings.Default.User + ", it's getting late"); }
                          break;

                      case "whats my name":
                      case "what is my name":
                          Alexis.SpeakAsync(Settings.Default.User);
                          break;

                      case "stop talking":
                      case "quit talking":
                          Alexis.SpeakAsyncCancelAll();
                          ranNum = rnd.Next(1, 2);
                          if (ranNum == 2)
                          { Alexis.Speak("sorry " + Settings.Default.User); }
                          break;
                    }
                }
          
          

instead of using the commands in the code. I recommend that you use a text document. once you have that then you can add your own commands to it then put it in code. Also reference the System.Speech.

I hope this helps on getting you on the right track.

2
  • I'm not really sure if this addresses my question in any way. It doesn't even appear to be a WinRT code sample. With regards to the language I'm using - I tagged the question as both C# and Windows Runtime. Commented Mar 1, 2015 at 16:17
  • Well i code in C# not WinRT but i suppose it should not be any different. I code digital assistants in winforms. To be honest you really can't tell anything about the code you provided. What names spaces, events, methods are you using? Are you only using Synthesis or are you using Recognition as well? Whats the purpose of the app. Is it only talking to you when an event is executed, or can you talk to it. If its a full speech program. Put all your commands in a text doc. then use the ReadLine to pull the commands. I can show you an example if you would like. Then you can turn it into WinRT.
    – Ian Massey
    Commented Mar 1, 2015 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.