转换的音频数据URI字符串到文件 [英] Convert audio data uri string to file

查看:424
本文介绍了转换的音频数据URI字符串到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器保存的音频数据为base64数据串。移动Web客户端获取数据并播放音频。

The server saves the audio data as base64 data string. The mobile web client fetches the data and plays the audio.

但iOS中发现了一个问题,在移动Chrome和Android,随着数据的URI音频无法播放(<一个href=\"http://stackoverflow.com/questions/32448411/audio-using-data-uri-displays-error-in-mobile-chrome\">issue).

But found an issue in mobile Chrome in iOS and android that the audio with data uri can't play (issue).

要使它的工作,我在想,如果有一个在客户端的方式将数据字符串转换为音频文件(如.m4a)将和音频SRC链接到的文件?

To make it work, I was wondering if there is a way in the client side to convert the data string to an audio file (like .m4a) and link the audio src to the file?

推荐答案

想通了,直接使用网络音频API已经在跨越iOS和Android的移动浏览器兼容性最好。

Figured out directly using the web audio api has the best compatibility across the mobile browsers in iOS and Android.

function base64ToArrayBuffer(base64) {
  var binaryString =  window.atob(base64);
  var len = binaryString.length;
  var bytes = new Uint8Array( len );
  for (var i = 0; i < len; i++)        {
    bytes[i] = binaryString.charCodeAt(i);
  }
  return bytes.buffer;
}

var base64 = '<data string retrieved from server>';
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
var source = audioContext.createBufferSource();
audioContext.decodeAudioData(base64ToArrayBuffer(base64), function(buffer) {
   source.buffer = buffer;
   source.connect(audioContext.destination);
   source.start(0);
});

它工作在iOS的Safari浏览器,Chrome和Android默认浏览器和Chrome。

It works in iOS safari, Chrome and Android default browser and Chrome.

这篇关于转换的音频数据URI字符串到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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