首页 > 解决方案 > In node on Windows 10, how can I get a list of all the voices installed?

问题描述

I've tried using say js with the following code:

 let voices = await say.getInstalledVoices((e, v) => {console.log(v)});
  console.log('voices=', voices);

Although the console.log in the callback function returns an array of voices, the voices variable returns a null.

I've had a go at wrapping the call in a promise and got a similar result.

Surely there's a simple one line way of getting those voices? I'm happy to use something other than say to do so.

标签: node.jscallbacktext-to-speech

解决方案


您不应将 await 用于该方法。该方法say.getInstalledVoices返回无效

只需使用以下语法来获取可用声音的列表:

const say = require('say');

function getVoices() {
  return new Promise((resolve) => {
    say.getInstalledVoices((err, voice) => {
      return resolve(voice)
    })
  })
}
async function usingVoices() {
  const voicesList = await getVoices();
  console.log(voicesList)
}
usingVoices()

usingVoices()您可以使用以下语法并在方法中做任何您想做的事情


推荐阅读