You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现输入文本(如姓名)自动语音播报并指定美音女声配置

Hey there! Let's walk through how to make your system speak a custom greeting when a user enters their name—using exactly the US English Female voice configuration you specified. Here's a hands-on implementation:

Step 1: Set Up the Input UI

First, create a simple input field for users to type their name, plus a trigger button. Add this HTML to your page:

<input type="text" id="nameInput" placeholder="Enter your name">
<button onclick="speakGreeting()">Say Hello</button>
Step 2: Implement Text-to-Speech with Your Specified Voice

We'll use the browser's built-in Web Speech API to handle the text-to-speech conversion. We'll filter available voices to match your target configuration, then generate the greeting. Add this JavaScript:

// Your specified US English Female voice configuration
const targetVoiceConfig = {
  name: "US English Female",
  flag: "us",
  gender: "f",
  voiceIDs: [39, 40, 41, 42, 43, 173, 205, 204, 235, 283, 339, 44]
};

let availableVoices = [];

// Load available voices when the browser is ready
window.speechSynthesis.onvoiceschanged = () => {
  availableVoices = window.speechSynthesis.getVoices();
};

function speakGreeting() {
  const userName = document.getElementById('nameInput').value.trim();
  
  // Make sure the user entered a name
  if (!userName) {
    alert("Please enter your name first!");
    return;
  }

  // Create the greeting text with the user's name
  const greeting = `Hello ${userName}, how are you today?`;

  // Find the matching voice from available options
  const targetVoice = availableVoices.find(voice => {
    // Check if the voice ID is in your specified list, or if the name matches
    const voiceIdNum = parseInt(voice.id);
    return targetVoiceConfig.voiceIDs.includes(voiceIdNum) || voice.name.includes(targetVoiceConfig.name);
  });

  // Fallback to any en-US female voice if the exact target isn't found
  const selectedVoice = targetVoice || availableVoices.find(v => v.lang === 'en-US' && v.name.includes('Female'));

  if (!selectedVoice) {
    console.warn("US English Female voice not available—using default system voice instead.");
  }

  // Set up the speech utterance
  const utterance = new SpeechSynthesisUtterance(greeting);
  utterance.voice = selectedVoice;
  utterance.lang = 'en-US';
  utterance.rate = 1; // Adjust speech speed (0.1 to 10) if needed
  utterance.pitch = 1; // Adjust pitch (0 to 2) if needed

  // Start speaking!
  window.speechSynthesis.speak(utterance);
}
Step 3: Key Notes for Reliability
  • Voice ID Variability: Voice IDs can differ across browsers (Chrome uses numerical IDs, Firefox uses string-based IDs). The code checks both IDs and voice names to maximize compatibility.
  • Fallback Logic: If your exact specified voice isn't available on the user's system, it'll fall back to another en-US female voice, or the default system voice as a last resort.
  • Browser Support: The Web Speech API works in most modern browsers (Chrome, Edge, Safari, Firefox), but double-check support for older versions if needed.
Step 4: Integrating with Existing Audio Code

If you already have sample audio code in place, you can adapt the speech synthesis logic to work alongside it—but the Web Speech API is the most direct way to generate dynamic, text-based speech using your specified voice configuration.

内容的提问来源于stack exchange,提问作者B.Ashcroft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:57:16