2015 ソリューション用に更新されました:
皆さん、ios6+ の Web オーディオの問題に取り組んでいる場合は、これらのリンクがヘルプとして見つかりました。
-これはコード ソリューションを含む優れた記事です: 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
-以下は、2 番目の記事の変更を使用して、最初の記事に対する私の更新されたソリューションです。私が抱えていた問題は、奇妙な not-enough-args エラーをスローする iOS 7 サファリでした。これで修正されました:
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;
});