Skip to content

Commit

Permalink
Fix audio library to integrate promise on play
Browse files Browse the repository at this point in the history
  • Loading branch information
llaske committed Apr 9, 2018
1 parent 63f389b commit 89bca91
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 61 deletions.
23 changes: 18 additions & 5 deletions activities/Abecedarium.activity/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enyo.kind({
create: function() {
this.inherited(arguments);

this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
this.startPlay = false;
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
Expand All @@ -29,7 +31,7 @@ enyo.kind({

// Handle volume buttons on Android
handleVolumeButtons: function() {
if ((enyo.platform.android || enyo.platform.androidChrome) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova && !enyo.platform.iOS) {
// HACK: Need only on Android because Cordova intercept volume buttons
var emptyf = function() {};
document.addEventListener("volumeupbutton", function() {
Expand Down Expand Up @@ -105,7 +107,7 @@ enyo.kind({
// Play audio
play: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
// Compute full path
var database = Abcd.context.getDatabase();
var src = ((database.length == 0 || this.src.indexOf("database") == -1 ) ? location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src : this.src);
Expand Down Expand Up @@ -155,13 +157,22 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.play();
this.started = false;
var promise = node.play();
if (promise) {
var that = this;
promise.then(function() {
that.started = true;
}).catch(function() {});
} else {
this.started = true;
}
},

// Pause audio
pause: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
if (!this.media)
return;
this.media.src = "";
Expand All @@ -172,7 +183,9 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.pause();
if (this.started) {
node.pause();
}
},

// Test if audio is paused
Expand Down
23 changes: 18 additions & 5 deletions activities/FoodChain.activity/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enyo.kind({
create: function() {
this.inherited(arguments);

this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
this.startPlay = false;
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
Expand All @@ -29,7 +31,7 @@ enyo.kind({

// Handle volume buttons on Android
handleVolumeButtons: function() {
if ((enyo.platform.android || enyo.platform.androidChrome) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova && !enyo.platform.iOS) {
// HACK: Need only on Android because Cordova intercept volume buttons
var emptyf = function() {};
document.addEventListener("volumeupbutton", function() {
Expand Down Expand Up @@ -105,7 +107,7 @@ enyo.kind({
// Play audio
play: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android < 4.4 and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
// Compute full path
var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
var that = this;
Expand All @@ -131,13 +133,22 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.play();
this.started = false;
var promise = node.play();
if (promise) {
var that = this;
promise.then(function() {
that.started = true;
}).catch(function() {});
} else {
this.started = true;
}
},

// Pause audio
pause: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
if (!this.media)
return;
this.media.src = "";
Expand All @@ -148,7 +159,9 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.pause();
if (this.started) {
node.pause();
}
},

// Test if audio is paused
Expand Down
4 changes: 0 additions & 4 deletions activities/FoodChain.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
define(["sugar-web/activity/activity","webL10n","sugar-web/graphics/radiobuttonsgroup","sugar-web/datastore"], function (activity, _l10n, radioButtonsGroup, datastore) {
l10n = _l10n;
var app = null;
var isFirefoxOS = (navigator.userAgent.indexOf('Mozilla/5.0 (Mobile') != -1);

// Manipulate the DOM only when it is ready.
require(['domReady!'], function (doc) {
Expand Down Expand Up @@ -50,9 +49,6 @@ define(["sugar-web/activity/activity","webL10n","sugar-web/graphics/radiobuttons
}
};
window.addEventListener('localized', localized_received, false);
if (enyo.platform.firefox || isFirefoxOS) {
localized_received();
}

// Stop sound at end of game to sanitize media environment, specifically on Android
document.getElementById("stop-button").addEventListener('click', function (event) {
Expand Down
4 changes: 2 additions & 2 deletions activities/FoodChain.activity/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ __$FC = function(str) {
if (value != undefined && value != "")
return value.replace("%27", "'").replace("%22", '"');
if (str == "sounddir") // HACK: At first launch not always initialized
return l10n.language.code;
return l10n.language.code.substr(0, 2);
return str;
}
}
8 changes: 4 additions & 4 deletions activities/FoodChain.activity/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FoodChain.saveContext = function() {
});
datastoreObject.setDataAsText(jsonData);
datastoreObject.save(function() {
});
});
};
FoodChain.loadContext = function(callback) {
var datastoreObject = FoodChain.activity.getDatastoreObject();
Expand Down Expand Up @@ -54,8 +54,8 @@ FoodChain.goHome = function() {
FoodChain.setLocale = function() {
document.getElementById("en-button").classList.remove('active');
document.getElementById("fr-button").classList.remove('active');
if (l10n.language.code == "en") document.getElementById("en-button").classList.add('active');
else if (l10n.language.code == "fr") document.getElementById("fr-button").classList.add('active');
if (l10n.language.code.indexOf("en") == 0) { document.getElementById("en-button").classList.add('active'); }
else if (l10n.language.code.indexOf("fr") == 0) { document.getElementById("fr-button").classList.add('active'); }
if (FoodChain.context.object != null)
FoodChain.context.object.setLocale();
}
Expand Down Expand Up @@ -120,4 +120,4 @@ FoodChain.getZoomLevel = function() {
zoom = 1;
}
return zoom;
};
};
62 changes: 37 additions & 25 deletions activities/LastOneLoses.activity/js/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ enyo.kind({
onSoundEnded: "",
onSoundTimeupdate: ""
},

// Constructor
create: function() {
this.inherited(arguments);


this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
this.startPlay = false;
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
Expand All @@ -29,20 +31,20 @@ enyo.kind({
// Render
rendered: function() {
this.inherited(arguments);

// Handle init
if (this.hasNode()) {
if (this.hasNode()) {
// Handle sound ended event
var audio = this;
enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
audio.doSoundEnded();
});
enyo.dispatcher.listen(audio.hasNode(), "timeupdate", function(s) {
});
enyo.dispatcher.listen(audio.hasNode(), "timeupdate", function(s) {
audio.doSoundTimeupdate({timeStamp: s.timeStamp});
});
});
}
},

// Property changed
srcChanged: function() {
this.setAttribute("src", this.src);
Expand All @@ -51,36 +53,36 @@ enyo.kind({
crossoriginChanged: function() {
this.setAttribute("crossorigin", this.crossorigin);
},

preloadChanged: function() {
this.setAttribute("preload", this.preload);
},

loopChanged: function() {
this.setAttribute("loop", this.loop);
},

mutedChanged: function() {
if (this.muted.length != 0)
this.setAttribute("muted", this.muted);
},

controlsbarChanged: function() {
this.setAttribute("controls", this.controlsbar);
},

// Test if component could play a file type
canPlayType: function(typename) {
var node = this.hasNode();
if (!node)
return false;
return node.canPlayType(typename);
},

// Play audio
play: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
var that = this;
var media = new Media(src, function() {
Expand All @@ -91,35 +93,45 @@ enyo.kind({
}, function() {});
media.play();
return;
}
}
var node = this.hasNode();
if (!node)
return;
node.play();
this.started = false;
var promise = node.play();
if (promise) {
var that = this;
promise.then(function() {
that.started = true;
}).catch(function() {});
} else {
this.started = true;
}
},

// Pause audio
pause: function() {
var node = this.hasNode();
if (!node)
return;
node.pause();
return;
if (this.started) {
node.pause();
}
},

// Test if audio is paused
paused: function() {
var node = this.hasNode();
if (!node)
return false;
return false;
return node.paused;
},

// Test if audio is ended
ended: function() {
var node = this.hasNode();
if (!node)
return false;
return false;
return node.ended;
}
}
});

23 changes: 18 additions & 5 deletions activities/TamTamMicro.activity/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ enyo.kind({
create: function() {
this.inherited(arguments);

this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
this.startPlay = false;
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
Expand All @@ -28,7 +30,7 @@ enyo.kind({

// Handle volume buttons on Android
handleVolumeButtons: function() {
if ((enyo.platform.android || enyo.platform.androidChrome) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova && !enyo.platform.iOS) {
// HACK: Need only on Android because Cordova intercept volume buttons
var emptyf = function() {};
document.addEventListener("volumeupbutton", function() {
Expand Down Expand Up @@ -101,7 +103,7 @@ enyo.kind({
// Play audio
play: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
// Compute full path
var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
var that = this;
Expand All @@ -127,13 +129,22 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.play();
this.started = false;
var promise = node.play();
if (promise) {
var that = this;
promise.then(function() {
that.started = true;
}).catch(function() {});
} else {
this.started = true;
}
},

// Pause audio
pause: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
if (this.isCordova) {
if (!this.media)
return;
this.media.src = "";
Expand All @@ -144,7 +155,9 @@ enyo.kind({
var node = this.hasNode();
if (!node)
return;
node.pause();
if (this.started) {
node.pause();
}
},

// Test if audio is paused
Expand Down
Loading

0 comments on commit 89bca91

Please sign in to comment.