Skip to content

Commit eca6b61

Browse files
committed
1 parent 8ca1ce0 commit eca6b61

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/soundjs/webaudio/WebAudioPlugin.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ this.createjs = this.createjs || {};
185185
*/
186186
s._unlocked = false;
187187

188+
/**
189+
* The default sample rate used when checking for iOS compatibility. See {{#crossLink "WebAudioPlugin/_createAudioContext"}}{{/crossLink}}.
190+
* @property DEFAULT_SAMPLE_REATE
191+
* @type {number}
192+
* @default 44100
193+
* @static
194+
*/
195+
s.DEFAULT_SAMPLE_RATE = 44100;
188196

189197
// Static Public Methods
190198
/**
@@ -276,13 +284,8 @@ this.createjs = this.createjs || {};
276284
if (t.canPlayType == null) {return null;}
277285

278286
if (s.context == null) {
279-
if (window.AudioContext) {
280-
s.context = new AudioContext();
281-
} else if (window.webkitAudioContext) {
282-
s.context = new webkitAudioContext();
283-
} else {
284-
return null;
285-
}
287+
s.context = s._createAudioContext();
288+
if (s.context == null) { return null; }
286289
}
287290
if (s._scratchBuffer == null) {
288291
s._scratchBuffer = s.context.createBuffer(1, 1, 22050);
@@ -298,7 +301,6 @@ this.createjs = this.createjs || {};
298301
document.addEventListener("touchend", s._unlock, true);
299302
}
300303

301-
302304
s._capabilities = {
303305
panning:true,
304306
volume:true,
@@ -321,6 +323,42 @@ this.createjs = this.createjs || {};
321323
}
322324
};
323325

326+
/**
327+
* Create an audio context for the sound.
328+
*
329+
* This method handles both vendor prefixes (specifically webkit support), as well as a case on iOS where
330+
* audio played with a different sample rate may play garbled when first started. The default sample rate is
331+
* 44,100, however it can be changed using the {{#crossLink "WebAudioPlugin/DEFAULT_SAMPLE_RATE:property"}}{{/crossLink}}.
332+
* @method _createAudioContext
333+
* @return {AudioContext | webkitAudioContext}
334+
* @private
335+
* @static
336+
* @since 1.0.0
337+
*/
338+
s._createAudioContext = function() {
339+
// Slightly modified version of https://github.com/Jam3/ios-safe-audio-context
340+
// Resolves issues with first-run contexts playing garbled on iOS.
341+
var AudioCtor = (window.AudioContext || window.webkitAudioContext),
342+
context = new AudioCtor();
343+
344+
// Check if hack is necessary. Only occurs in iOS6+ devices
345+
// and only when you first boot the iPhone, or play a audio/video
346+
// with a different sample rate
347+
if (/(iPhone|iPad)/i.test(navigator.userAgent)
348+
&& context.sampleRate !== s.DEFAULT_SAMPLE_RATE) {
349+
var buffer = context.createBuffer(1, 1, s.DEFAULT_SAMPLE_RATE),
350+
dummy = context.createBufferSource();
351+
dummy.buffer = buffer;
352+
dummy.connect(context.destination);
353+
dummy.start(0);
354+
dummy.disconnect();
355+
context.close() // dispose old context
356+
357+
context = new AudioCtor();
358+
}
359+
return context;
360+
}
361+
324362
/**
325363
* Set up compatibility if only deprecated web audio calls are supported.
326364
* See http://www.w3.org/TR/webaudio/#DeprecationNotes

0 commit comments

Comments
 (0)