Skip to content

Commit

Permalink
Stop audio that's playing when another audio playback gets started
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Jan 22, 2020
1 parent 1ec141e commit 40d3832
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/scripts/h5p-dictation-button.js
Expand Up @@ -16,6 +16,8 @@ class Button {
* @param {number} params.tries Maximum number of tries before disabling button;
* @param {string} params.sample Path to sound sample.
* @param {number} params.type Type of the sample (0 = normal, 1 = slow);
* @param {object} params.callbacks Callbacks.
* @param {function} params.callbacks.playAudio PlayAudio callback.
* @param {object} previousState PreviousState.
*/
constructor(id, params, previousState = {}) {
Expand All @@ -33,6 +35,8 @@ class Button {
this.params.a11y.solution = this.params.a11y.solution || 'Solution';
this.params.a11y.enterText = this.params.a11y.enterText || 'Enter what you have heard';
this.params.type = this.params.type || Button.BUTTON_TYPE_NORMAL;
this.params.callbacks = this.params.callbacks || {};
this.params.callbacks.playAudio = this.params.callbacks.playAudio || (() => {});

this.triesLeft = (typeof previousState.triesLeft !== 'undefined') ? previousState.triesLeft : this.params.maxTries;

Expand Down Expand Up @@ -104,6 +108,8 @@ class Button {
}

this.status = Button.STATUS_PLAYING;

this.params.callbacks.playAudio(this);
});

// Event Listener Pause
Expand Down Expand Up @@ -177,6 +183,15 @@ class Button {
}
}

/**
* Pause.
*/
pause() {
if (this.status === Button.STATUS_PLAYING) {
this.button.click();
}
}

/**
* Decrease the number of tries and disable button if necessary.
*/
Expand Down
31 changes: 29 additions & 2 deletions src/scripts/h5p-dictation-sentence.js
Expand Up @@ -32,6 +32,9 @@ class Sentence {
this.maxTriesAlternative = params.triesAlternative;

this.params.sentence.description = (this.params.sentence.description || '').trim();
this.params.callbacks = this.params.callbacks || {};
this.params.callbacks.playAudio = this.params.callbacks.playAudio || (() => {});

this.solutionText = Util.htmlDecode(params.sentence.text).trim();
this.solutionText = (!params.ignorePunctuation) ? this.solutionText : this.stripPunctuation(this.solutionText);
this.containsRTL = (this.params.overrideRTL === 'auto') ?
Expand Down Expand Up @@ -61,7 +64,12 @@ class Sentence {
audioNotSupported: params.audioNotSupported,
type: Button.BUTTON_TYPE_NORMAL,
maxTries: params.tries,
a11y: params.a11y
a11y: params.a11y,
callbacks: {
playAudio: (button) => {
this.params.callbacks.playAudio(button);
}
}
},
previousState.buttonPlayNormal
);
Expand All @@ -76,7 +84,12 @@ class Sentence {
audioNotSupported: params.audioNotSupported,
type: Button.BUTTON_TYPE_SLOW,
maxTries: params.triesAlternative,
a11y: params.a11y
a11y: params.a11y,
callbacks: {
playAudio: (button) => {
this.params.callbacks.playAudio(button);
}
}
},
previousState.buttonPlaySlow
);
Expand Down Expand Up @@ -279,6 +292,20 @@ class Sentence {
}
}

/**
* Pause buttons.
* @param {Button} [excludeButton] Button to ignore.
*/
pauseButtons(excludeButton) {
if (this.buttonPlayNormal && this.buttonPlayNormal !== excludeButton) {
this.buttonPlayNormal.pause();
}

if (this.buttonPlaySlow && this.buttonPlaySlow !== excludeButton) {
this.buttonPlaySlow.pause();
}
}

/**
* Focus button.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/scripts/h5p-dictation.js
Expand Up @@ -132,6 +132,11 @@ class Dictation extends H5P.Question {
alternateSolution: this.params.behaviour.alternateSolution,
overrideRTL: this.params.behaviour.overrideRTL,
autosplit: this.params.behaviour.autosplit,
callbacks: {
playAudio: (button) => {
this.handlePlayAudio(button);
}
}
},
this.contentId,
previousState)
Expand Down Expand Up @@ -229,6 +234,16 @@ class Dictation extends H5P.Question {
}, false, {}, {});
};

/**
* Handle playing audio.
* @param {Button} button Calling button.
*/
this.handlePlayAudio = (button) => {
this.sentences.forEach(sentence => {
sentence.pauseButtons(button);
});
};

/**
* Show the evaluation for the input in the text input fields.
*/
Expand Down

0 comments on commit 40d3832

Please sign in to comment.