科大讯飞 Websockets API 的问题?
尝试调用科大讯飞的实时语音流式转写接口:https://www.xfyun.cn/doc/asr/voicedictation/API.html
xunfei = XunFei(APPID='...', APISecret='...', AudioFile=r'test.pcm')url = xunfei.create_url()
async def hello():
async with websockets.connect(url) as websocket:
cc = xunfei.read_audio()
print("数据:" + json.dumps(cc))
await websocket.send(str(json.dumps(cc)))
while True:
message = await websocket.recv()
print(f"收到消息: {json.loads(message)}")
if __name__ == "__main__":
asyncio.run(hello())
但老是报错
Traceback (most recent call last): File "/var/www/QBA/config/voice.py", line 135, in <module>
asyncio.run(hello())
File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/var/www/QBA/config/voice.py", line 131, in hello
message = await websocket.recv()
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 568, in recv
await self.ensure_open()
File "/usr/local/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 944, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: received 1000 (OK) Websocket closed!,sid=iat000dc91f@dx18ad01d4a38a11f802!reason=server read msg timeout; then sent 1000 (OK) Websocket closed!,sid=iat000dc91f@dx18ad01d4a38a11f802!reason=server read msg timeout
不懂这是为什么,但是我发个普通的消息就会立马得到反馈
回答:
是我自己的问题,在发送数据的时候必须有 3 种数据包:012 分别代表起始帧、中间、结束、否则就会没响应直接结束
以上是 科大讯飞 Websockets API 的问题? 的全部内容, 来源链接: utcz.com/p/939025.html