为 2015 年解决方案更新:
大家好,如果您在这里使用 ios6+ 解决网络音频问题,我发现这些链接可以帮助您。
- 这是一篇带有代码解决方案的好文章:http: //matt-harrison.com/perfect-web-audio-on-ios-devices-with-the-web-audio-api/
- 这是上述 ^ 解决方案文章编写后对 api 的更新https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
- 下面是我对第一篇文章的更新解决方案,使用了第二篇文章的更改。我遇到的问题是 iOS 7 Safari 抛出了一个奇怪的 not-enough-args 错误。这修复了它:
define(function() {
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
window.audioContext = new window.AudioContext();
} catch (e) {
console.log("No Web Audio API support");
}
/*
* WebAudioAPISoundManager Constructor
*/
var WebAudioAPISoundManager = function (context) {
this.context = context;
this.bufferList = {};
this.playingSounds = {};
};
/*
* WebAudioAPISoundManager Prototype
*/
WebAudioAPISoundManager.prototype = {
addSound: function (url) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var self = this;
request.onload = function () {
// Asynchronously decode the audio file data in request.response
self.context.decodeAudioData(
request.response,
function (buffer) {
if (!buffer) {
alert('error decoding file data: ' + url);
return;
}
self.bufferList[url] = buffer;
});
};
request.onerror = function () {
alert('BufferLoader: XHR error');
};
request.send();
},
stopSoundWithUrl: function(url) {
if(this.playingSounds.hasOwnProperty(url)){
for(var i in this.playingSounds[url]){
if(this.playingSounds[url].hasOwnProperty(i)) {
this.playingSounds[url][i].stop(0);
}
}
}
}
};
/*
* WebAudioAPISound Constructor
*/
var WebAudioAPISound = function (url, options) {
this.settings = {
loop: false
};
for(var i in options){
if(options.hasOwnProperty(i)) {
this.settings[i] = options[i];
}
}
this.url = '/src/www/assets/audio/' + url + '.mp3';
this.volume = 1;
window.webAudioAPISoundManager = window.webAudioAPISoundManager || new WebAudioAPISoundManager(window.audioContext);
this.manager = window.webAudioAPISoundManager;
this.manager.addSound(this.url);
// this.buffer = this.manager.bufferList[this.url];
};
/*
* WebAudioAPISound Prototype
*/
WebAudioAPISound.prototype = {
play: function () {
var buffer = this.manager.bufferList[this.url];
//Only play if it's loaded yet
if (typeof buffer !== "undefined") {
var source = this.makeSource(buffer);
source.loop = this.settings.loop;
source.start(0);
if(!this.manager.playingSounds.hasOwnProperty(this.url)) {
this.manager.playingSounds[this.url] = [];
}
this.manager.playingSounds[this.url].push(source);
}
},
stop: function () {
this.manager.stopSoundWithUrl(this.url);
},
getVolume: function () {
return this.translateVolume(this.volume, true);
},
//Expect to receive in range 0-100
setVolume: function (volume) {
this.volume = this.translateVolume(volume);
},
translateVolume: function(volume, inverse){
return inverse ? volume * 100 : volume / 100;
},
makeSource: function (buffer) {
var source = this.manager.context.createBufferSource();
var gainNode = this.manager.context.createGain();
source.connect(gainNode);
gainNode.gain.value = this.volume;
source.buffer = buffer;
// source.connect(gainNode);
gainNode.connect(this.manager.context.destination);
return source;
}
};
return WebAudioAPISound;
});