Skip to content

Commit

Permalink
Route to restore audioContext (if suspended on safari or other?)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgere committed Jun 13, 2019
1 parent d9176a0 commit 88c5d9e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Router = EmberRouter.extend({

Router.map(function() {
this.route('box', {path : 'box/:version_idx'});
this.route('unlock-audio');
});

export default Router;
32 changes: 32 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export default Route.extend({

intl: service(),
ajaxService: service('ajax'),
audioService: service('audio'),

audioUnlockPreviousTransition: null,

beforeModel() {
this._super(...arguments);
Expand All @@ -33,4 +36,33 @@ export default Route.extend({

return samplesConf;
},

afterModel(model, transition) {
this._super(model, transition);
let audioContext = this.get('audioService.audioContext');
if (audioContext.state !== 'suspended'){
return;
}

if( transition.targetName != 'unlock-audio'){
//Deal with "suspended" audio Context on Safari
transition.abort();
this.set('audioUnlockPreviousTransition', transition);
this.transitionTo('unlock-audio');
}
},

actions: {
replayInitialeTransition: function(){
let transition = this.get('audioUnlockPreviousTransition');
if( transition){
this.set('audioUnlockPreviousTransition', null);
transition.retry();
}
else{
this.transitionTo('index');
}
}
}

});
42 changes: 42 additions & 0 deletions app/routes/unlock-audio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
/**
* https://www.mattmontag.com/web/unlock-web-audio-in-safari-for-ios-and-macos
*/
export default Route.extend({

audioService: service('audio'),
events : ['touchstart','touchend', 'mousedown','keydown'],

handleEvent: function(){
this.unlock();
},

afterModel(model, transition) {
this._super(model, transition);

const b = document.body;
this.get('events').forEach((e) => {
b.addEventListener(e, this, false);
});
},

unlock: function(){

this.get('audioService.audioContext').resume().then(() => {

//Remove touch/click event
this.clean();

//Replay previous transition
this.send('replayInitialeTransition');
});
},

clean: function(){
const b = document.body;
this.get('events').forEach((e) => {
b.removeEventListener(e, this, false);
});
},
});
1 change: 1 addition & 0 deletions app/templates/unlock-audio.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Votre navigateur a suspendu l'accès audio, veuillez cliquez/toucher l'écran pour débloquer l'audio et utiliser l'application.
11 changes: 11 additions & 0 deletions tests/unit/routes/unlock-audio-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('route:unlock-audio', 'Unit | Route | unlock audio', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});

test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});

0 comments on commit 88c5d9e

Please sign in to comment.