如何让Amazon Polly播放TTS音频。 React-Native,Expo-Av

时间:2019-08-29 08:57:16

标签: react-native expo amazon-polly

我正在尝试创建一个文本到语音组件。就Polly而言,我认为她运行正常(尽管由于无法确定我无法测试音频),而且我还可以通过expo-av播放本地声音。我无法做的部分是让玩家播放Amazon Polly创建的TTS。我认为这可能与我对Amazon Polly的工作原理缺乏了解有关。到目前为止,这是我的代码:

var AWS = require('aws-sdk');
import { Audio } from 'expo-av';



playLocalSound = async() =>{

    console.log("Hiya");

    Audio.setIsEnabledAsync(true);

    const soundObject = new Audio.Sound();

        await soundObject.loadAsync(require('./Test1.mp3'))
        .catch(err=>{console.log(err)})
        .then(response =>{console.log(response)});

        await soundObject.playAsync().catch(err=>{console.log(err)})
        .catch(err=>{console.log(err)})
        .then(response =>{console.log(response)});



} //This function works as expected - I have sound hurray!


playSound = async(x) =>{
    Audio.setIsEnabledAsync(true);

    const soundObject = new Audio.Sound();
//res.AudioStream
console.log("made it this far");
        await soundObject.loadAsync(x)
        .catch(err=>{console.log(err+" loading sound error")}) //This is where I get the errors.
        .then(response =>{console.log(response)});

        await soundObject.playAsync().catch(err=>{console.log(err)})
        .catch(err=>{console.log(err+" playing sound error")})
        .then(response =>{console.log(response)});
}


var Polly = new AWS.Polly({
    region: 'eu-west-1',
    accessKeyId: 'RemovedforThisStackOVerflowQuestion',
    secretAccessKey: 'RemovedforThisStackOVerflowQuestion' 
});



var speak = async(text) => {

    var params = { OutputFormat: 'pcm', VoiceId: 'Raveena', Text: text }
 console.log(params);
    await Polly.synthesizeSpeech(params, async function(err, res) {
        if (err) {
            console.log('polly err', err)
        } else {

            console.log(res);


            console.log("Hiya");
        playSound(res.AudioStream);

        }
    })
}
module.exports = { Speak: speak };

此行发生错误:

await soundObject.loadAsync(x)

有错误:

Error: Cannot load an AV asset from a null playback source

一如既往,我们将不胜感激。预先感谢。

0 个答案:

没有答案