使用微软Azure文本转语音第三方库替换掉script src=httpscdnjsdelivrnetnpmtext-to-speech-jsdisttext-to-speechminjsscript重新写出来代码body h1汉字检测器h1 div class=input-box textarea class=textarea placeholder=请输入需要检测的汉字textare
<body>
<h1>汉字检测器</h1>
<div class="input-box">
<textarea class="textarea" placeholder="请输入需要检测的汉字"></textarea>
<div class="btn-wrap">
<button class="btn check-btn">检测</button>
<button class="btn clear-btn">清空</button>
</div>
</div>
<div class="output"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/microsoft-cognitiveservices-speech-sdk/distrib/browser/microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
<script>
const textarea = document.querySelector('.textarea');
const checkBtn = document.querySelector('.check-btn');
const clearBtn = document.querySelector('.clear-btn');
const output = document.querySelector('.output');
<pre><code>checkBtn.addEventListener('click', async () => {
const content = textarea.value.trim();
const reg = /[\u4e00-\u9fa5]/g;
const result = (content.match(reg) || []).length;
output.innerText = `输入的文本中共包含 ${result} 个汉字`;
// 计算时间长度
if (result > 0) {
const speechConfig = SpeechSDK.SpeechConfig.fromSubscription('<your-subscription-key>', '<your-service-region>');
const synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, undefined);
const result = await synthesizer.speakTextAsync(content);
const time = (result.duration / (60 * 10000000)).toFixed(2);
output.innerText += `\n如果配音,大概需要 ${time} 分钟`;
} else {
output.innerText += '\n没有汉字需要配音';
}
});
clearBtn.addEventListener('click', () => {
textarea.value = '';
output.innerText = '';
});
</code></pre>
</script>
</body>
原文地址: https://www.cveoy.top/t/topic/bmJf 著作权归作者所有。请勿转载和采集!