HFP-1145 Stop audio when turning another card

pull/43/head
Frode Petterson 2019-01-02 11:40:27 +01:00
parent e0d639c406
commit ca4d37c85c
2 changed files with 29 additions and 17 deletions

35
card.js
View File

@ -66,11 +66,13 @@
var handlePlaying = function () { var handlePlaying = function () {
if ($card) { if ($card) {
$card.addClass('h5p-memory-audio-playing'); $card.addClass('h5p-memory-audio-playing');
self.trigger('audioplay');
} }
}; };
var handleStopping = function () { var handleStopping = function () {
if ($card) { if ($card) {
$card.removeClass('h5p-memory-audio-playing'); $card.removeClass('h5p-memory-audio-playing');
self.trigger('audiostop');
} }
}; };
audioPlayer.addEventListener('play', handlePlaying); audioPlayer.addEventListener('play', handlePlaying);
@ -115,24 +117,20 @@
return; return;
} }
if (audioPlayer) {
audioPlayer.play();
}
$card.addClass('h5p-flipped'); $card.addClass('h5p-flipped');
self.trigger('flip'); self.trigger('flip');
flippedState = true; flippedState = true;
if (audioPlayer) {
audioPlayer.play();
}
}; };
/** /**
* Flip card back. * Flip card back.
*/ */
self.flipBack = function () { self.flipBack = function () {
if (audioPlayer) { self.stopAudio();
audioPlayer.pause();
audioPlayer.currentTime = 0;
}
self.updateLabel(null, null, true); // Reset card label self.updateLabel(null, null, true); // Reset card label
$card.removeClass('h5p-flipped'); $card.removeClass('h5p-flipped');
flippedState = false; flippedState = false;
@ -150,11 +148,7 @@
* Reset card to natural state * Reset card to natural state
*/ */
self.reset = function () { self.reset = function () {
if (audioPlayer) { self.stopAudio();
audioPlayer.pause();
audioPlayer.currentTime = 0;
}
self.updateLabel(null, null, true); // Reset card label self.updateLabel(null, null, true); // Reset card label
flippedState = false; flippedState = false;
removedState = false; removedState = false;
@ -236,8 +230,7 @@
$card.children('.h5p-back') $card.children('.h5p-back')
.click(function () { .click(function () {
if ($card.hasClass('h5p-memory-audio-playing')) { if ($card.hasClass('h5p-memory-audio-playing')) {
audioPlayer.pause(); self.stopAudio();
audioPlayer.currentTime = 0;
} }
else { else {
audioPlayer.play(); audioPlayer.play();
@ -289,6 +282,16 @@
self.isRemoved = function () { self.isRemoved = function () {
return removedState; return removedState;
}; };
/**
* Stop any audio track that might be playing.
*/
self.stopAudio = function () {
if (audioPlayer) {
audioPlayer.pause();
audioPlayer.currentTime = 0;
}
};
}; };
// Extends the event dispatcher // Extends the event dispatcher

View File

@ -22,7 +22,7 @@ H5P.MemoryGame = (function (EventDispatcher, $) {
// Initialize event inheritance // Initialize event inheritance
EventDispatcher.call(self); EventDispatcher.call(self);
var flipped, timer, counter, popup, $bottom, $taskComplete, $feedback, $wrapper, maxWidth, numCols; var flipped, timer, counter, popup, $bottom, $taskComplete, $feedback, $wrapper, maxWidth, numCols, audioCard;
var cards = []; var cards = [];
var flipBacks = []; // Que of cards to be flipped back var flipBacks = []; // Que of cards to be flipped back
var numFlipped = 0; var numFlipped = 0;
@ -218,6 +218,9 @@ H5P.MemoryGame = (function (EventDispatcher, $) {
*/ */
var addCard = function (card, mate) { var addCard = function (card, mate) {
card.on('flip', function () { card.on('flip', function () {
if (audioCard) {
audioCard.stopAudio();
}
// Always return focus to the card last flipped // Always return focus to the card last flipped
for (var i = 0; i < cards.length; i++) { for (var i = 0; i < cards.length; i++) {
@ -260,6 +263,12 @@ H5P.MemoryGame = (function (EventDispatcher, $) {
// Count number of cards turned // Count number of cards turned
counter.increment(); counter.increment();
}); });
card.on('audioplay', function () {
audioCard = card;
});
card.on('audiostop', function () {
audioCard = undefined;
});
/** /**
* Create event handler for moving focus to the next or the previous * Create event handler for moving focus to the next or the previous