_ctypes.COMError: (-2147024809, '参数错误。', (None, None, None, 0, None))

 

报错代码:

stream.Open(outfile, SpeechLib.SSFMCreateForWrite)

import socket
import subprocess
from comtypes.client import CreateObject
import pythoncom
from comtypes.gen import SpeechLib

engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
def send_voice(data,host,port):

    pythoncom.CoInitialize()

    t1 = time.time()
    outfile = "welcome.wav"
    stream.Open(outfile, SpeechLib.SSFMCreateForWrite)

 

原因:开了多线程并发调用就会报错,解决:加sleep,顺序调用就可以了。

# coding:utf-8
import threading
import time
#方法一:将要执行的方法作为参数传给Thread的构造方法
def action(arg):
    data = "你好,gasd,小马"
    host = '192.168.25.110'
    port = 6002
    send_voice(data, host, port)


if __name__ == '__main__':
    for i in range(3):
        t =threading.Thread(target=action,args=(i,))
        t.start()
        time.sleep(3)

你可能感兴趣的:(python)