Skip to content

Commit

Permalink
Merge pull request #737 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
pygame音频加载播放增加异常捕获,给予用户一定报错问题的原因猜测
  • Loading branch information
Ikaros-521 committed Mar 27, 2024
2 parents 50005d2 + e95fb63 commit 2d5af38
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name 直播弹幕监听 转发至本地WS服务端
// @namespace http://tampermonkey.net/
// @version 0.1
// @version 0.5
// @description 观察指定 DOM 节点的变化以将数据发送到连接的WebSocket服务端
// @description Github:https://github.com/Ikaros-521/AI-Vtuber/tree/main/Scripts/%E7%9B%B4%E6%92%ADws%E8%84%9A%E6%9C%AC
// @author Ikaros
Expand Down Expand Up @@ -37,7 +37,12 @@
console.log("ws连接打开");

// 向服务器发送一条消息
socket.send("ws连接成功");
const data = {
type: "info",
content: "ws连接成功",
};
console.log(data);
socket.send(JSON.stringify(data));
});

// 当收到消息时触发
Expand Down
35 changes: 23 additions & 12 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,12 +1036,17 @@ async def only_play_audio(self):
Audio.audio_player.play(data_json)
else:
logging.debug(f"voice_tmp_path={voice_tmp_path}")
# 使用pygame播放音频
Audio.mixer_normal.music.load(voice_tmp_path)
Audio.mixer_normal.music.play()
while Audio.mixer_normal.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_normal.music.stop()
try:
# 使用pygame播放音频
Audio.mixer_normal.music.load(voice_tmp_path)
Audio.mixer_normal.music.play()
while Audio.mixer_normal.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_normal.music.stop()
except pygame.error as e:
logging.error(traceback.format_exc())
# 如果发生 pygame.error 异常,则捕获并处理它
logging.error(f"无法加载音频文件:{voice_tmp_path}。请确保文件格式正确且文件未损坏。可能原因是TTS配置有误或者TTS服务端有问题,可以去服务端排查一下问题")

# 是否启用字幕输出
#if captions_config["enable"]:
Expand Down Expand Up @@ -1139,12 +1144,18 @@ async def random_speed_and_play(audio_path):
}
Audio.audio_player.play(data_json)
else:
# 使用pygame播放音频
Audio.mixer_copywriting.music.load(audio_path)
Audio.mixer_copywriting.music.play()
while Audio.mixer_copywriting.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_copywriting.music.stop()
try:
# 使用pygame播放音频
Audio.mixer_copywriting.music.load(audio_path)
Audio.mixer_copywriting.music.play()
while Audio.mixer_copywriting.music.get_busy():
pygame.time.Clock().tick(10)
Audio.mixer_copywriting.music.stop()
except pygame.error as e:
logging.error(traceback.format_exc())
# 如果发生 pygame.error 异常,则捕获并处理它
logging.error(f"无法加载音频文件:{voice_tmp_path}。请确保文件格式正确且文件未损坏。可能原因是TTS配置有误或者TTS服务端有问题,可以去服务端排查一下问题")


# 添加延时,暂停执行n秒钟
await asyncio.sleep(float(self.config.get("copywriting", "audio_interval")))
Expand Down

0 comments on commit 2d5af38

Please sign in to comment.