Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created October 31, 2012 21:42
Show Gist options
  • Save chrisallick/3990089 to your computer and use it in GitHub Desktop.
Save chrisallick/3990089 to your computer and use it in GitHub Desktop.
Autoplay audio on the ipad or iphone using webkitaudiocontext instead of audio tag
AudioSFX = function( _p, _autoplay ) {
var self = this;
var parent = _p;
this.sounds = new Array();
this.context = new webkitAudioContext();
this.autoplay = _autoplay;
this.play = function( sound ) {
var source = self.context.createBufferSource();
source.buffer = self.sounds[sound];
source.connect( self.context.destination );
source.noteOn( 0 );
}
this.load = function() {
var request = new XMLHttpRequest();
request.addEventListener( 'load', function(e) {
self.context.decodeAudioData( request.response, function(decoded_data) {
self.sounds[0] = decoded_data;
if( self.autoplay ) {
self.play(0);
}
}, function(e){
console.log("error");
});
}, false);
request.open( 'GET', 'wav/sfx.wav', true );
request.responseType = "arraybuffer";
request.send();
}
self.load();
}
var sfx = new AudioSFX( this, true ); // true for autoplay
@karneaud
Copy link

doesn't seem to work

@chrisallick
Copy link
Author

really? they might have updated safari, but this was working in production. do you have a sample project?

@willian12345
Copy link

it seems to be didn't work
i had tested it

test it in on ios 6 safari
http://sugarcane-mall.com/x/test.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment