views:

363

answers:

3
var speechEngine = new SpVoiceClass();
SetVoice(speechEngine, job.Voice);

var fileMode = SpeechStreamFileMode.SSFMCreateForWrite;
var fileStream = new SpFileStream();
try
{
    fileStream.Open(filePath, fileMode, false);
    speechEngine.AudioOutputStream = fileStream;
    speechEngine.Speak(job.Script, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFDefault); //TODO: Change to XML
    //Wait for 15 minutes only
    speechEngine.WaitUntilDone((uint)new TimeSpan(0, 15, 0).TotalMilliseconds);
}
finally
{
    fileStream.Close();
}

This exact code works in a WinForm app, but when I run it inside a webservice I get the following

System.Runtime.InteropServices.COMException was unhandled
  Message="Exception from HRESULT: 0x80045003"
  Source="Interop.SpeechLib"
  ErrorCode=-2147201021

Does anyone have any ideas what might be causing this error? The error code means

SPERR_UNSUPPORTED_FORMAT

For completeness here is the SetVoice method

void SetVoice(SpVoiceClass speechEngine, string voiceName)
{
    var voices = speechEngine.GetVoices(null, null);
    for (int index = 0; index < voices.Count; index++)
    {
     var currentToken = (SpObjectToken)voices.Item(index);
     if (currentToken.GetDescription(0) == voiceName)
     {
      speechEngine.SetVoice((ISpObjectToken)currentToken);
      return;
     }
    }
    throw new Exception("Voice not found: " + voiceName);
}

I have given full access to USERS on the folder C:\Temp where the file is to be written. Any help would be appreciated!

+1  A: 

I don't think the System.Speech works in windows service. It looks like there is a dependency to Shell, which isn't available to services. Try interop with SAPI's C++ interfaces. Some class in System.Runtime.InteropServices may help on that.

Sheng Jiang 蒋晟
A: 

Make sure you explicitly set the format on the SPFileStream object. ISpAudio::SetState (which gets called in a lower layer from speechEngine.Speak) will return SPERR_UNSUPPORTED_FORMAT if the format isn't supported.

Eric Brown
A: 

I just got the webservice to spawn a console app to do the processing. PITA :-)

Peter Morris