使用WebAudio API在iOS 7.1中变形的audio

在iOS 7.1上,当使用Web Audio API播放audio时,我总是发出嗡嗡/嘈杂/失真的声音 。 这听起来像是这样扭曲了 ,取而代之的是正常的 。

使用HTML5audio时,相同的文件是好的。 这一切在桌面上运行良好(Firefox,Chrome,Safari)。

编辑:

  • audio在iOS模拟器iOS 7.1,8.1,8.2中失真。 嗡嗡声往往开始之前,我甚至回放任何东西。
  • 在Chrome和Safari中,audio在运行iOS 7.1的物理iPhone上失真。
  • 在Chrome和Safari中,运行iOS 8.1的物理iPhone上的audio效果都不错。

即:嗡嗡声在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() */ 

这个代码的现场版本可以在这里find: Web Audio API – Hello world

谷歌没有提出任何有关iOS 7.1上这种扭曲的声音问题的结果。

有没有其他人遇到它? 我应该向苹果提交一个错误报告吗?

我相信这个问题是由于重置audioContext.sampleRate道具引起的,这个道具在浏览器/操作系统以不同的采样率播放内容之后似乎会发生。

我已经devise了下面的解决方法,它基本上默默地​​播放一个简短的wav文件,以设备当前播放的采样率logging:

 "use strict"; var getData = function( context, filePath, callback ) { var source = context.createBufferSource(), request = new XMLHttpRequest(); request.open( "GET", filePath, 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 ); } ); }; 

显然,如果某些设备的采样率不同,则需要检测每个采样率并使用特定的文件。

它看起来像iOS6 +的Safari默认为48000的采样率。如果您第一次打开移动Safari时,在开发人员控制台中键入此,您将得到48000:

 var ctx = new window.webkitAudioContext(); console.log(ctx.sampleRate); 

进一步参考: https : //forums.developer.apple.com/thread/20677

然后,如果closures加载的初始上下文: ctx.close() ,则下一个创build的上下文将使用大多数其他浏览器使用的采样率(44100),并且声音将无失真地播放。

相信这一点是为了指引我朝着正确的方向发展(以防万一以上情况再次发生): https : //github.com/Jam3/ios-safe-audio-context/blob/master/index.js

function截止date:

 function createAudioContext (desiredSampleRate) { var AudioCtor = window.AudioContext || window.webkitAudioContext desiredSampleRate = typeof desiredSampleRate === 'number' ? desiredSampleRate : 44100 var context = new AudioCtor() // Check if hack is necessary. Only occurs in iOS6+ devices // and only when you first boot the iPhone, or play a audio/video // with a different sample rate if (/(iPhone|iPad)/i.test(navigator.userAgent) && context.sampleRate !== desiredSampleRate) { var buffer = context.createBuffer(1, 1, desiredSampleRate) var dummy = context.createBufferSource() dummy.buffer = buffer dummy.connect(context.destination) dummy.start(0) dummy.disconnect() context.close() // dispose old context context = new AudioCtor() } return context }