Skip to content

Commit

Permalink
Answering a question plays the appropriate sound (ding or buzzer)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchirls committed Apr 5, 2012
1 parent bc750c6 commit 83484c8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
Binary file added audio/buzzer.mp3
Binary file not shown.
Binary file added audio/buzzer.ogg
Binary file not shown.
Binary file added audio/ding.mp3
Binary file not shown.
Binary file added audio/ding.ogg
Binary file not shown.
57 changes: 54 additions & 3 deletions js/plugins/popcorn.quiz.js
Expand Up @@ -3,7 +3,9 @@
"use strict";

var styleSheet,
console = window.console;
console = window.console,
sounds,
isiPad = navigator.userAgent.match(/iPad/i);

Popcorn.basePlugin( 'quiz' , function(options, base) {
var popcorn = this,
Expand All @@ -13,7 +15,49 @@
question,
explanation;

function loadSounds() {
var name, sound, i, obj, source, rewind;

if (sounds || isiPad) {
//already started loading
return;
}

rewind = function() {
this.currentTime = 0;
};

sounds = {
right: {
urls: [
'audio/ding.mp4',
'audio/ding.ogg'
]
},
wrong: {
urls: [
'audio/buzzer.mp4',
'audio/buzzer.ogg'
]
}
};

for (name in sounds) {
obj = sounds[name];
obj.audio = document.createElement('audio');
obj.audio.preload = true;
obj.audio.addEventListener('ended', rewind, false);
for (i = 0; i < obj.urls.length; i++) {
source = document.createElement('source');
source.src = obj.urls[i];
obj.audio.appendChild(source);
}
}
}

function clickAnswer(i) {
var status;

if (answer >= 0) {
//don't re-answer this until reset
return;
Expand All @@ -24,13 +68,18 @@

base.addClass(answers[i].label.parentNode, 'answered');
if (base.options.correct === i) {
base.addClass(base.container, 'right');
status = 'right';
options.correct = true;
} else {
base.addClass(base.container, 'wrong');
status = 'wrong';
options.correct = false;
}

base.addClass(base.container, status);
if (sounds && sounds[status] && sounds[status].audio && sounds[status].audio.networkState > 0) {
sounds[status].audio.play();
}

if (typeof options.onAnswer === 'function') {
if (Popcorn.plugin.debug) {
options.onAnswer(options);
Expand Down Expand Up @@ -58,6 +107,8 @@
return;
}

loadSounds();

guid = 'question-' + Popcorn.guid();

if (!styleSheet) {
Expand Down

0 comments on commit 83484c8

Please sign in to comment.