Reputation: 149
Hi I want to use Microsoft Speech API SAPI with python 3.7.
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")
I am working with Remotedesktop. I start my python script over RDP and its working fine and I can hear the text to speech output. But after I close RDP it stops working.
I can configure my RDP connection to play sounds either on the remote desktop PC or on my working PC. Both is working fine, but only as long as RDP is connected. After I close RDP both variants are no longer working.
I am getting this error in my python log file after I have closed RDP:
speaker.Speak("Hello, it works!")
File "<COMObject SAPI.SpVoice>", line 3, in Speak
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147200925), None)
I have tried to set AudioOutput, because i thought this could cause the problem:
print(len(speaker.GetAudioOutputs()))
for k in speaker.GetAudioOutputs():
print(k.GetDescription())
speaker.AudioOutput = speaker.GetAudioOutputs()[0]
As long as RDP is running I can see the Audio Output device in the list of GetAudioOutputs(). But after I have closed RDP connection, there is no longer any output device available. len(speaker.GetAudioOutputs()) is 0. Of course I do have a working Audio Device on my remote PC, but it is not showing up.
It is working fine with other remote desktop solutions like Chrome Remote Desktop or Anydesk but only not with Microsofts RDP. Unfortunately I do need Microsofts RDP.
I have no idea how I can make it work.
I thought maybe I do have to "reset" SAPI each time, so that the change of audio devices can be recognized after I have closed RDP.
Any help is really appreciated.
Upvotes: 0
Views: 595
Reputation: 13932
I did a quick google search on the error code -2147200925, which resolves to SPERR_NOT_ACTIVE_SESSION. This error is generated because SAPI will not perform audio input or output once the terminal session has disconnected.
Your only choices are to either use a different remote desktop protocol (e.g., VNC) that doesn't use user session switching, or move to the WinRT speech synthesis APIs, which don't make this check.
Since you're using Python, I did some googling, and found the winrt package on pypl that appears to make interfacing to WinRT APIs straightforward. I can't vouch further for this; I haven't used it at all.
Upvotes: 1