diff --git a/index.html b/index.html index 83c9da28..29cbdef0 100644 --- a/index.html +++ b/index.html @@ -4,8 +4,8 @@ Bloc Jams - - + + @@ -36,11 +36,11 @@

- + - +
@@ -71,7 +71,7 @@

- +
diff --git a/scripts/album-data.js b/scripts/album-data.js index 69e24bb8..808dcb2d 100644 --- a/scripts/album-data.js +++ b/scripts/album-data.js @@ -9,5 +9,6 @@ const album = { { title: 'Red', duration: '268.45', soundFileUrl: 'assets/music/red.mp3' }, { title: 'Pink', duration: '153.14', soundFileUrl: 'assets/music/pink.mp3' }, { title: 'Magenta', duration: '374.22', soundFileUrl: 'assets/music/magenta.mp3' } + ] -}; \ No newline at end of file +}; diff --git a/scripts/album-info.js b/scripts/album-info.js index e69de29b..0b3417fc 100644 --- a/scripts/album-info.js +++ b/scripts/album-info.js @@ -0,0 +1,6 @@ +{ +$('#album-title').text(album.title); +$('img#album-cover-art').attr('src', album.albumArtUrl); +$(".artist").text(album.artist); +$('#release-info').text(album.releaseInfo); +} diff --git a/scripts/player-bar.js b/scripts/player-bar.js index e69de29b..0baa4209 100644 --- a/scripts/player-bar.js +++ b/scripts/player-bar.js @@ -0,0 +1,58 @@ +{ + $('button#play-pause').on('click', function() { + player.playPause(); + $(this).attr('playState', player.playState); + }); + + $('button#next').on('click', function() { + if (player.playState !== 'playing') { return; } + + const currentSongIndex = album.songs.indexOf(player.currentlyPlaying); + const nextSongIndex = currentSongIndex + 1; + if (nextSongIndex >= album.songs.length) { return; } + + const nextSong = album.songs[nextSongIndex]; + player.playPause(nextSong); + }); + + $('button#previous').on('click', function() { + if (player.playState !== 'playing') { return; } + + + const currentSongIndex = album.songs.indexOf(player.currentlyPlaying); + const previousSongIndex = currentSongIndex - 1; + if (currentSongIndex == 0) { return; } + + const previousSong = album.songs[previousSongIndex]; + player.playPause(previousSong); + }); + + $('#time-control input').on('input', function (event) { + player.skipTo(event.target.value); + }); + +$('#volume-control input').on('input', function (event) { + player.setVolume(event.target.value); +}); + + + setInterval( () => { + if (player.playState !== 'playing') { return; } + + var currentTime = player.getTime(); + + const duration = player.getDuration(); + const percent = (currentTime / duration) * 100; + var totalTime = duration - currentTime; + + currentTime = player.prettyTime(currentTime); + totalTime = player.prettyTime(totalTime); + + $('#time-control .current-time').text( currentTime ); + $('#time-control .total-time').text( totalTime); + $('#time-control input').val(percent); + + + }, 1000); + +} diff --git a/scripts/player.js b/scripts/player.js index 0d81b305..aec8ad3b 100644 --- a/scripts/player.js +++ b/scripts/player.js @@ -13,14 +13,14 @@ class Player { getTime() { return this.soundObject.getTime(); } - + playPause (song = this.currentlyPlaying) { if (this.currentlyPlaying !== song) { // Stop the currently playing sound file (even if nothing is playing) this.soundObject.stop(); // Clear classes on the song that's currently playing this.currentlyPlaying.element.removeClass('playing paused'); - + // Update our currentlyPlaying and playState properties this.currentlyPlaying = song; this.playState = 'stopped'; @@ -37,17 +37,25 @@ class Player { this.currentlyPlaying.element.removeClass('playing').addClass('paused'); } } - + skipTo (percent) { if (this.playState !== 'playing') { return } this.soundObject.setTime( (percent / 100) * this.soundObject.getDuration() ); } - + setVolume (percent) { this.volume = percent; this.soundObject.setVolume(percent); } + + prettyTime (timeInSeconds) { + var minutes = Math.floor(timeInSeconds / 60); + var seconds = Math.floor(timeInSeconds - minutes * 60); + if (seconds < 10) { + seconds = "0" + seconds; + } + return minutes + ":" + seconds; + } } const player = new Player(); - diff --git a/scripts/song-list.js b/scripts/song-list.js index e69de29b..56ab9b5e 100644 --- a/scripts/song-list.js +++ b/scripts/song-list.js @@ -0,0 +1,23 @@ +{ + album.songs.forEach( (song, index) => { + song.element = $(` + + + + ${song.title} + ${player.prettyTime(song.duration)} + + `); + + song.element.on('click', event => { + player.playPause(song); + $('button#play-pause').attr('playState', player.playState); + }); + + $('#song-list').append(song.element); + }); +}