反应原生。 MP3二进制字符串(Uint8Array(9549))流或文件 [英] React Native. MP3 Binary String (Uint8Array(9549)) to stream or file

查看:527
本文介绍了反应原生。 MP3二进制字符串(Uint8Array(9549))流或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to play an audio file with binary string format that Amazon Polly returns. For that, I am using 'react-native-fetch-blob' and reading a stream, but just keep getting errors from the bridge saying 'Invalid data message - all must be length: 8'. It happens when I try to open the stream: 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});
        })  
    })
}); 

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

Another solution I have also tried is to save the stream into a file to load it later on, but I got similars bugs. RNFetchBlob.fs.createFile("myfile.mp3", dataG.AudioStream, 'ascii');

提前非常感谢

推荐答案

您可以使用 AWS.Polly.Presigner 。我这样做并使用react-native-sound来播放mp3。我遇到了一个不能播放mp3的问题,因为我的预先签名的URL包含特殊字符,但是有一个在此处修复

You could use the getSynthesizeSpeechUrl method from AWS.Polly.Presigner. I’m doing this and using react-native-sound to play the mp3. I ran into an issue where the mp3 wouldn’t play because my presigned URL contained special characters, but there’s a fix here.

这篇关于反应原生。 MP3二进制字符串(Uint8Array(9549))流或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆