Google 文本转语音 nodejs

Google text-to-speech nodejs

我正在尝试编写一个使用 google tts api 的 nodejs 应用程序,我的问题是,returns 一个 url 音频。我需要能够自动听到文本,而无需转到 link 并播放音频。

只需要 url 和 "play it" – 这是一个 link 音频文件。使用 play-sound 的示例:

const googleTTS = require("google-tts-api");
const player = require("play-sound")();

googleTTS("Hello World", "en", 1).then(url => player.play(url));

play-sound 包通过执行外部播放器来工作——参见 #options for a list. You can even specify another one with the player option. The player needs to support playing from https urls, obviously. I tried it with mpv 并且它工作得很好。

如果您不能或不想使用外部播放器,则需要获取音频,从响应中获取数据缓冲区并以某种方式播放。所以沿着这条路走下去:

googleTTS("Hello World", "en", 1).then(url => {
  fetch(url)
    .then(response => response.buffer())
    .then(buffer => playWithSomething(buffer));
});

首先,安装 mpv 播放器然后试试这个 ==>

const googleTTS = require("google-tts-api");
let mpv = require('node-mpv');
let mpvPlayer = new mpv();
googleTTS("Hello world", "en",1).then(url => mpvPlayer.load(url));