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; + } +