<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='1.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 () =&gt; {
  const content = textarea.value.trim();
  const reg = /[一-龥]/g;
  const result = (content.match(reg) || []).length;
  output.innerText = `输入的文本中共包含 ${result} 个汉字`;

  // 计算时间长度
  if (result &gt; 0) {
    const speechConfig = SpeechSDK.SpeechConfig.fromSubscription('&lt;your-subscription-key&gt;', '&lt;your-service-region&gt;');
    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', () =&gt; {
  textarea.value = '';
  output.innerText = '';
});
</code></pre>
  </script>
</body>
<p>需要引入 Microsoft Cognitive Services Speech SDK 库,将其添加到代码中即可。具体方法请参考该库的官方文档。</p>
JavaScript 报错 'Cannot read properties of undefined (reading 'duration')' 解决方法

原文地址: https://www.cveoy.top/t/topic/mPdD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录