我正在编写一个带有音频播放组件的平台。音频作为 wav/mp3/ogg 文件上传到服务器,然后(像我们的其他媒体一样)转换为 base64 并存储在我们的 redis 数据库中。
为了在客户端播放音频,我们向服务器发出 AJAX 请求以获取 base64 编码的音频。我们有一个与移动应用程序互补的桌面版本,目前音频播放是这样的:
recording.sound = new Audio("data:audio/ogg;base64," + recording.audio);
recording.sound.play(); // this works
今天我们开始在移动设备上进行测试,但到目前为止还无法正常运行,即使在显然支持 HTML5 音频的移动浏览器上也是如此。
有什么想法吗?或者,如果这不可能,我们是否可以采取不同的方法?理想情况下,应该有 Web 应用程序的移动兼容版本,并且必须有 phonegap 版本。
最佳答案
这里的原因可能不是技术原因,来自 Apple 开发者网站:
In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.
这同样适用于 Android 设备。
关于javascript - 移动设备上的 HTML5 base64 编码音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19893919/