3

我正在尝试播放 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');

非常感谢提前

4

2 回答 2

1

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

于 2017-10-10T13:40:10.743 回答
0

您可以使用fetch()来请求一个或多个媒体资源,返回Response.body.getReader().then()以获取ReadableStream响应。使用 的方法Uint8Array读取作为流返回的值,附加到 的值以在中流式传输媒体。.read()ReadableStreamSourceBufferMediaSourceHTMLMediaElement

例如,要依次输出两个请求的音频资源的音频

window.addEventListener("load", () => {

  const audio = document.createElement("audio");

  audio.controls = "controls";

  document.body.appendChild(audio);

  audio.addEventListener("canplay", e => {
    audio.play();
  });

  const words = ["hello", "world"];

  const mediaSource = new MediaSource();

  const mimeCodec = "audio/mpeg";

  const mediaType = ".mp3";

  const url = "https://ssl.gstatic.com/dictionary/static/sounds/de/0/";

  Promise.all(
      words.map(word =>
        fetch(`https://query.yahooapis.com/v1/public/yql?q=select * from data.uri where url="${url}${word}${mediaType}"&format=json&callback=`)
        .then(response => response.json())
        .then(({
            query: {
              results: {
                url
              }
            }
          }) =>
          fetch(url).then(response => response.body.getReader())
          .then(readers => readers)
        )
      )
    )
    .then(readers => {

      audio.src = URL.createObjectURL(mediaSource);
      mediaSource.addEventListener("sourceopen", sourceOpen);

      async function sourceOpen() {
        var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
        // set `sourceBuffer` `.mode` to `"sequence"`
        sourceBuffer.mode = "segments";

        const processStream = ({
          done,
          value
        }) => {
          if (done) {
            return;
          }
          // append chunk of stream to `sourceBuffer`
          sourceBuffer.appendBuffer(value);
        }
        // at `sourceBuffer` `updateend` call `reader.read()`,
        // to read next chunk of stream, append chunk to 
        // `sourceBuffer`
        for (let [index, reader] of Object.entries(readers)) {
          sourceBuffer.addEventListener("updateend", function() {
            reader.read().then(processStream);
          });

          let stream = await reader.read().then(processStream)
            .then(() => reader.closed)
            .then(() => "done reading stream " + index);

          console.log(stream);
        }

      }
    })
}) 

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

于 2017-08-04T19:02:57.343 回答