Skip to content

Commit

Permalink
fix: 支持长内容自动滚动,修复LocalPlayer播放问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wzpan committed Apr 8, 2023
1 parent 63d0e78 commit 3121fb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions robot/Conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _ttsAction(self, msg, cache, index, onCompleted=None):
return voice
except Exception as e:
logger.error(f"语音合成失败:{e}", stack_info=True)
self.tts_index += 1
traceback.print_exc()
return None

Expand Down Expand Up @@ -264,13 +265,11 @@ def appendHistory(self, t, text, UUID="", plugin=""):
)

def _onCompleted(self, msg):
pass
pass

def pardon(self):
if not self.hasPardon:
self.say(
"抱歉,刚刚没听清,能再说一遍吗?", cache=True
)
self.say("抱歉,刚刚没听清,能再说一遍吗?", cache=True)
self.hasPardon = True
else:
self.say("没听清呢")
Expand All @@ -284,10 +283,12 @@ def _tts_line(self, line, cache, index=0, onCompleted=None):
:param index: 合成序号
:param onCompleted: 播放完成的操作
"""
line = line.strip()
pattern = r"http[s]?://.+"
if re.match(pattern, line):
logger.info("内容包含URL,屏蔽后续内容")
return None
line.replace("- ", "")
if line:
result = self._ttsAction(line, cache, index, onCompleted)
return result
Expand Down
13 changes: 8 additions & 5 deletions robot/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def __init__(self, **kwargs):
self.thread_loop = threading.Thread(target=self.loop.run_forever)
self.thread_loop.start()

def executeOnCompleted(self, onCompleted):
def executeOnCompleted(self, res, onCompleted):
# 全部播放完成,播放统一的 onCompleted()
onCompleted and onCompleted()
res and onCompleted and onCompleted()
if self.play_queue.empty():
for onCompleted in self.onCompleteds:
onCompleted and onCompleted()
Expand All @@ -100,10 +100,12 @@ def playLoop(self):
with self.play_lock:
logger.info(f"开始播放音频:{src}")
self.src = src
self.doPlay(src)
# 将 onCompleted() 方法的调用放到事件循环的线程中执行
res = self.doPlay(src)
self.play_queue.task_done()
self.loop.call_soon_threadsafe(self.executeOnCompleted, onCompleted)
# 将 onCompleted() 方法的调用放到事件循环的线程中执行
self.loop.call_soon_threadsafe(
self.executeOnCompleted, res, onCompleted
)

def doPlay(self, src):
system = platform.system()
Expand All @@ -121,6 +123,7 @@ def doPlay(self, src):
if self.delete:
utils.check_and_delete(src)
logger.info(f"播放完成:{src}")
return self.proc and self.proc.returncode == 0

def play(self, src, delete=False, onCompleted=None):
if src and (os.path.exists(src) or src.startswith("http")):
Expand Down
2 changes: 2 additions & 0 deletions server/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ socket.onmessage = function (e) {
if (data.action === "new_message") {
// console.log("收到新消息: ", data);
showMessage(data)
let scrollHeight = $('.history').prop("scrollHeight");
$('.history').scrollTop(scrollHeight, 200);
}
};

Expand Down

0 comments on commit 3121fb3

Please sign in to comment.