So here's the actual event of the space bar. There's a search bar at the top of the page so obviously the space bar needs to work correctly if it has focus.
//Press spacebar to pause or play the current song
$(window).keypress(function(event)
{
if(event.which === 32 && !$('#search').is(':focus'))
{
event.preventDefault();
change_song(index);
}
});
The change_song() function is below.
function change_song(i)
{
var song = $('div.search-result:visible').eq(i);
var button = song.find('button');
var new_source = song.find('span.source').text();
var current_source = $('#player source').attr('src');
var cover = song.find('span.cover').text();
var title = song.find('span.song').text();
var year = song.find('span.year').text();
var album = song.find('span.album').text();
var genre = song.find('span.genre').text();
if(new_source != current_source)
{
$('img.cover').attr('src', cover);
$('#progress').attr('value', 0.0);
$('div.search-result button').removeClass('playing');
button.addClass('playing');
player.paused;
$('#player source').attr('src', new_source);
player.load();
player.play();
$('#title + label span').text(title);
$('#album + label span').text(album);
$('#year + label span').text(year);
$('#genre + label span').text(genre);
$('div.left, div.right').addClass('playing');
$('title').text(title);
var width = Math.round($('div.right').width() * 0.25);
var current_max_width = $('div.left').outerWidth();
if(width > current_max_width || $('div.left').attr('style') === undefined)
{
$('div.left').css('max-width', width + 'px');
}
}
else
{
if(player.paused)
{
player.play();
button.addClass('playing');
}
else
{
player.pause();
button.removeClass('playing');
}
}
}
It could use some cleanup, but for now it's "working as intended". The index is simply the index of the song on the page. Basically I have a PHP script that recursively searches through my music library collecting information about albums and songs (found a neat PHP library that reads metatag info) and generates an HTML file which I just include into my page. While I type in the search bar, it looks in certain spots (AKA, song title) and then hides or shows the results. Generating it beforehand allows it to be pretty much instant which is cool.
For reference this is the code for when the play/pause button is clicked:
//Click the play button
$('div.right').on('click', 'div.search-result button', function(event)
{
var button = $(this);
var parent = button.parent();
index = $('div.search-result:visible').index(parent);
change_song(index);
});
The only other time change_song() is called is 'ended' when the current song finishes. It increments the index and passes the new index to the change_song() function, effectively starting the next song since the sources are different, otherwise program flows to the "else" in the if statement.
Last night I also quickly tried adding an event listener to the audio tag (both via jQuery and through traditional JavaScript means) to listen for keypress, keydown, and keyup and to .preventDefault(), but it had no effect.
Also for reference here's some typical markup for a song on the page. I am using an image sprite for the button for "pause" and "play" images.
<div class="search-result song">
<button></button>
<span class="title">Metallica - Hit the Lights</span>
<span class="duration">4:18</span>
<span class="hidden source">music/Metallica [US]/[1983] Kill 'Em All/Metallica - 01 - Hit the Lights.mp3</span>
<span class="hidden type">audio/mpeg</span>
<span class="hidden cover">music/Metallica [US]/[1983] Kill 'Em All/cover.jpg</span>
<span class="hidden album">Kill 'Em All</span>
<span class="hidden band">Metallica</span>
<span class="hidden song">Hit the Lights</span>
<span class="hidden year">1983</span>
<span class="hidden genre">Thrash Metal</span>
<span class="hidden track">01</span>
</div>
Thank you for taking a look at this. If you need any more info just let me know.