Oliver Link

Oliver Link

  • NA
  • 2
  • 1.7k

NAudio waveout stops after half 1 second

Aug 22 2017 3:30 PM
Hey im using NAudio to playback SpeechSynthesizer with some strange result.
There are to different outputs the first works perfect the second is exactly the same just with an other input text but the playback stops after half 1 second and the PlaybackState never leave PlaybackState.Playing. This  happens also if both inputs are the same.
 
but lets have a look at my code:
 
 
  1. waveOut = new WaveOut();  
  2. if (SayText == "")  
  3. {  
  4.    MemoryStream stream = new MemoryStream();  
  5.    try  
  6.    {  
  7.       IWaveProvider provider = null;  
  8.       using (var synth = new SpeechSynthesizer())  
  9.       {  
  10.          synth.SetOutputToAudioStream(stream,  
  11.          new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));  
  12.          lock (textoutput)  
  13.          {  
  14.             synth.Speak(textoutput);  // Example: textoutput = Hallo
  15.          }  
  16.          stream.Seek(0, SeekOrigin.Begin);  
  17.          provider = new RawSourceWaveStream(stream, new WaveFormat(44100, 16, 2));  
  18.       }  
  19.       waveOut.DeviceNumber = getDevices("tts");  // getDevices returns 1
  20.       waveOut.Init(provider);  
  21.       waveOut.Play();  
  22.       do  {}
  23.       while (waveOut.PlaybackState == PlaybackState.Playing);  
  24.       waveOut.Dispose();  
  25.    }  
  26.    catch (System.Exception e)  
  27.    {  
  28.       MessageBox.Show("Error playing sound: " + e.Message);  
  29.    }  
  30. }  
// THIS PART MAKE SOME PROBLEMS
  1. else  
  2. {  
  3.    MemoryStream stream = new MemoryStream();  
  4.    try  
  5.    {  
  6.       IWaveProvider provider = null;  
  7.       using (var synth = new SpeechSynthesizer())  
  8.       {  
  9.          synth.SetOutputToAudioStream(stream,  
  10.          new SpeechAudioFormatInfo(44100, AudioBitsPerSample.Sixteen, AudioChannel.Mono));  
  11.          lock (SayText)  
  12.          {  
  13.          synth.Speak(SayText);  // Example: SayText = Hallo
  14.          }  
  15.          stream.Seek(0, SeekOrigin.Begin);  
  16.          provider = new RawSourceWaveStream(stream, new WaveFormat(44100, 16, 1));  
  17.       }  
  18.       waveOut.DeviceNumber = getDevices("say");  // getDevices returns 1
  19.       waveOut.Init(provider);  
  20.       waveOut.Play();  // after that step the waveout stops directly and the following loop never ends because waveOut.PlaybackState is always Playing
  21.       do  {} 
  22.       while (waveOut.PlaybackState == PlaybackState.Playing);  
  23.       waveOut.Dispose();  
  24.       }  
  25.       catch (System.Exception e)  
  26.       {  
  27.          MessageBox.Show("Error playing sound: " + e.Message);  
  28.       }  
  29.       SayText = "";  
  30. }