From 82758271e37161f15f81b5c374a260dd682838d1 Mon Sep 17 00:00:00 2001
From: xiongxiaoyang <1179705413@qq.com>
Date: Mon, 5 Feb 2024 12:05:26 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=AF=AD=E9=9F=B3?=
=?UTF-8?q?=E6=92=AD=E6=94=BE=E5=B0=8F=E8=AF=B4=E5=86=85=E5=AE=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../templates/mobile/book/book_content.html | 93 +++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/novel-front/src/main/resources/templates/mobile/book/book_content.html b/novel-front/src/main/resources/templates/mobile/book/book_content.html
index 0dee97c..59c91da 100644
--- a/novel-front/src/main/resources/templates/mobile/book/book_content.html
+++ b/novel-front/src/main/resources/templates/mobile/book/book_content.html
@@ -210,6 +210,26 @@
字体:大 中 小
+
+
+
+
@@ -389,6 +409,79 @@
}
+ console.log(speechSynthesis.getVoices());
+
+ $(window).on('beforeunload', function () {
+ if (speechSynthesis.speaking) {
+ speechSynthesis.cancel()
+ }
+ });
+
+ function speakChapter() {
+ console.log('speechSynthesis.paused', speechSynthesis.paused)
+ console.log('speechSynthesis.pending', speechSynthesis.pending)
+ console.log('speechSynthesis.speaking', speechSynthesis.speaking)
+ if (speechSynthesis.speaking && !speechSynthesis.paused) {
+ speechSynthesis.pause();
+ return;
+ }
+ if (speechSynthesis.speaking && speechSynthesis.paused) {
+ speechSynthesis.resume();
+ }
+ speak({
+ 'text': $('#chaptercontent').text(),
+ 'speechRate': 0.5,
+ 'pitch': 1,
+ 'lang': 'zh-CN'
+ }, function () {
+ console.log('语音播放结束');
+ }, function () {
+ console.log('语音开始播放');
+ });
+
+
+ }
+
+ /**
+ * @description 文字转语音方法
+ * @public
+ * @param { text, rate, lang, volume, pitch } object
+ * @param text 要合成的文字内容,字符串
+ * @param speechRate 读取文字的语速 0.1~10 正常1
+ * @param lang 读取文字时的语言
+ * @param volume 读取时声音的音量 0~1 正常1
+ * @param voice 读取文字的语音服务
+ * @param pitch 读取时声音的音高 0~2 正常1
+ * @returns SpeechSynthesisUtterance
+ */
+ function speak({text, speechRate, lang, volume, pitch, voice}, endEvent, startEvent) {
+ if (!window.SpeechSynthesisUtterance) {
+ console.warn('当前浏览器不支持文字转语音服务')
+ return;
+ }
+
+ if (!text) {
+ return;
+ }
+
+ const speechUtterance = new SpeechSynthesisUtterance();
+ speechUtterance.text = text;
+ speechUtterance.rate = speechRate || 1;
+ speechUtterance.lang = lang || 'zh-CN';
+ speechUtterance.volume = volume || 1;
+ speechUtterance.pitch = pitch || 1;
+ speechUtterance.voice = voice || null;
+ speechUtterance.onend = function () {
+ endEvent && endEvent();
+ };
+ speechUtterance.onstart = function () {
+ startEvent && startEvent();
+ };
+ speechSynthesis.speak(speechUtterance);
+
+ return speechUtterance;
+ }
+