javascript - react native 。 MP3 二进制字符串 (Uint8Array(9549)) 到流或文件

标签 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');

提前致谢

最佳答案

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

关于javascript - react native 。 MP3 二进制字符串 (Uint8Array(9549)) 到流或文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512358/

相关文章:

java - AWS Polly Java 示例

python - Amazon Polly ProfileNotFound : The config profile (adminuser) could not be found

javascript - 如何从 Controller 访问 ng-Quill 实例以更改 AngularJs 中的工具栏

android - 如何通过 React Native 在 Android WebView 中隐藏滚动条

reactjs - 为什么在 React Native 中我的页面转换之前有一个白色的 "loading screen"?

node.js - 如何将 Amazon polly TTS 和语音标记添加到 MAYA 中构建的 3D 模型中?

javascript - Babel 6 转换运行时 : $export is not a function

javascript - 在图片上显示图片

javascript - JS source map文件什么时候下载到客户端?

javascript - React Native - 如何在类组件中初始化对象?