Skip to content

Commit 2eb8587

Browse files
authored
Play/pause, next/prev with keyboard (#2)
1 parent 200aa80 commit 2eb8587

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

js/openmixtape.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,46 @@ $(function() {
9999
this.on('error', function(error) {
100100
console.log('error: ' + error.message);
101101
}, this);
102+
/* Keyboard */
103+
$(document).on('keypress', function (e) {
104+
console.log('key: ' + e.which);
105+
// Spacebar: Play/pause
106+
if (e.which == 32) {
107+
var current = $('ol li.playing');
108+
if (!current.length) {
109+
current = $('ol li').first();
110+
current.addClass('playing')
111+
audio5.load($('a', current).attr('data-src'));
112+
audio5.on('canplay', function () {
113+
audio5.play();
114+
}, this);
115+
} else {
116+
audio5.playPause();
117+
}
118+
}
119+
// j: previous
120+
if (e.which == 106) {
121+
audio5.pause();
122+
var prev = $('ol li.playing').prev();
123+
if (!prev.length) prev = $('ol li').first();
124+
prev.addClass('playing').siblings().removeClass('playing');
125+
audio5.load($('a', prev).attr('data-src'));
126+
audio5.on('canplay', function () {
127+
audio5.play();
128+
}, this);
129+
}
130+
// k: next
131+
if (e.which == 107) {
132+
audio5.pause();
133+
var next = $('ol li.playing').next();
134+
if (!next.length) next = $('ol li').first();
135+
next.addClass('playing').siblings().removeClass('playing');
136+
audio5.load($('a', next).attr('data-src'));
137+
audio5.on('canplay', function () {
138+
audio5.play();
139+
}, this);
140+
}
141+
});
102142
}
103143
});
104144
});

0 commit comments

Comments
 (0)