反应原生。用于流式传输或归档

时间:2017-08-04 17:20:08

标签: javascript react-native amazon-polly react-native-fetch-blob uint8array

我正在尝试播放Amazon Polly返回的二进制字符串格式的音频文件。 为此,我正在使用' react-native-fetch-blob'并且阅读流,但只是不断从桥上得到错误说“无效的数据信息 - 所有必须是长度:8'。 当我尝试打开流时会发生这种情况:ifstream.open()

这是代码:

//polly config
const params = {
    LexiconNames: [], 
    OutputFormat: "mp3", 
    SampleRate: "8000", 
    Text: "All Gaul is divided into three parts", 
    TextType: "text", 
    VoiceId: "Joanna"
};

Polly.synthesizeSpeech(params, function(err, data) {
    let _data = "";
    RNFetchBlob.fs.readStream(
        // file path
        data.AudioStream,
        // encoding, should be one of `base64`, `utf8`, `ascii`
        'ascii'
    )
    .then((ifstream) => {
        ifstream.open()
        ifstream.onData((chunk) => {
            _data += chunk
        })
        ifstream.onError((err) => {
            console.log('oops', err.toString())
        })
        ifstream.onEnd(() => {  
            //pasing _data to streaming player or normal audio player
            ReactNativeAudioStreaming.play(_data, {showIniOSMediaCenter: true, showInAndroidNotifications: true});
        })  
    })
}); 

我还尝试过的另一个解决方案是将流保存到文件中以便稍后加载它,但我得到了类似的错误。 RNFetchBlob.fs.createFile("myfile.mp3", dataG.AudioStream, 'ascii');

提前非常感谢

2 个答案:

答案 0 :(得分:0)

您可以使用fetch()请求一个或多个媒体资源,从Response.body.getReader()返回.then()以获得ReadableStream响应。阅读使用Uint8Array的{​​{1}}方法读取的流返回的.read()值,附加到ReadableStream SourceBuffer的值,以便在MediaSource

例如,按顺序输出两个请求的音频资源的音频

HTMLMediaElement

plnkr http://plnkr.co/edit/9zHwmcdG3UKYMghD0w3q?p=preview

答案 1 :(得分:0)

您可以使用AWS.Polly.Presigner中的getSynthesizeSpeechUrl方法。我这样做并使用react-native-sound来播放mp3。我遇到了一个不能播放mp3的问题,因为我的预签名网址包含特殊字符,但有一个fix here