在iOS 7.1上,当使用Web Audio API播放音频时,我不断发出嗡嗡声/嘈杂/失真的声音.这听起来是
distorted like this,而不是
normal like this.
使用HTML5音频时,相同的文件很好.这一切都适用于桌面(Firefox,Chrome,Safari).
编辑:
> iOS模拟器版本iOS 7.1,8.1,8.2中的音频失真.在我播放任何东西之前,嗡嗡的声音经常开始.
>在Chrome和Safari中运行iOS 7.1的物理iPhone上的音频失真.
>在Chrome和Safari中运行iOS 8.1的物理iPhone上的音频都很好.
即:嗡嗡声的音频在iOS 7.1上.只要.
Howler.js不是问题.问题仍然是使用纯JS这样:
var context; var sound; var extension = '.' + ( new Audio().canPlayType( 'audio/ogg' ) !== '' ? 'ogg' : 'mp3'); /** Test for WebAudio API support **/ try { // still needed for Safari window.AudioContext = window.AudioContext || window.webkitAudioContext; // create an AudioContext context = new AudioContext(); } catch(e) { // API not supported throw new Error( 'Web Audio API not supported.' ); } function loadSound( url ) { var request = new XMLHttpRequest(); request.open( 'GET',url,true ); request.responseType = 'arraybuffer'; request.onload = function() { // request.response is encoded... so decode it now context.decodeAudioData( request.response,function( buffer ) { sound = buffer; },function( err ) { throw new Error( err ); }); } request.send(); } function playSound(buffer) { var source = context.createBufferSource(); source.buffer = buffer; source.connect(context.destination); source.start(0); } loadSound( '/tests/Assets/Audio/En-us-hello' + extension ); $(document).ready(function(){ $( '#clickme' ).click( function( event ) { playSound(sound); }); }); /* END .ready() */
此代码的实时版本可在此处获得:Web Audio API – Hello world
谷歌没有提出有关iOS 7.1上这种扭曲声音问题的任何结果.
还有其他人遇到过吗?我应该向Apple提交错误报告吗?
解决方法
我认为这个问题是由于重置audioContext.sampleRate prop引起的,这似乎是在浏览器/ OS播放以不同采样率记录的内容之后发生的.
我已经设计了以下解决方法,它基本上默默地播放以设备当前播放的采样率记录的短wav文件:
"use strict"; var getData = function( context,filePath,callback ) { var source = context.createBufferSource(),request = new XMLHttpRequest(); request.open( "GET",true ); request.responseType = "arraybuffer"; request.onload = function() { var audioData = request.response; context.decodeAudioData( audioData,function( buffer ) { source.buffer = buffer; callback( source ); },function( e ) { console.log( "Error with decoding audio data" + e.err ); } ); }; request.send(); }; module.exports = function() { var AudioContext = window.AudioContext || window.webkitAudioContext,context = new AudioContext(); getData( context,"path/to/short/file.wav",function( bufferSource ) { var gain = context.createGain(); gain.gain.value = 0; bufferSource.connect( gain ); gain.connect( context.destination ); bufferSource.start( 0 ); } ); };
显然,如果某些设备具有不同的采样率,则需要针对每个速率检测并使用特定文件.