科大讯飞 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
不懂这是为什么,但是我发个普通的消息就会立马得到反馈
回复
1个回答

test
2024-06-26
是我自己的问题,在发送数据的时候必须有 3 种数据包:012 分别代表起始帧、中间、结束、否则就会没响应直接结束
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容