Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 18 sliders #122

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ <h2 class="artist"></h2>
<col id="song-number-column">
<col id="song-title-column">
<col id="song-duration-column">
</colgroup>
</colgroup>
</table>

</main>

<!-- Player Bar -->
<section id="player-bar">
<section id="buttons">
Expand Down Expand Up @@ -71,7 +71,7 @@ <h2 class="artist"></h2>
<div class="icon ion-volume-high"></div>
</div>


</section>

<!-- Scripts -->
Expand All @@ -82,5 +82,6 @@ <h2 class="artist"></h2>
<script src="scripts/album-info.js"></script>
<script src="scripts/song-list.js"></script>
<script src="scripts/player-bar.js"></script>
<script src="scripts/helper.js"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions scripts/album-info.js
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions scripts/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Helper {

playPauseAndUpdate(song) {
player.playPause(song);
let totalTime = 'null';
if (song !== undefined){
totalTime =song.duration;
} else {
totalTime = player.getDuration();
}

$('#time-control .total-time').text(totalTime);

};

}
const helper = new Helper();
51 changes: 51 additions & 0 deletions scripts/player-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
$('button#play-pause').on('click', function() {
helper.playPauseAndUpdate();
$(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];
helper.playPauseAndUpdate(nextSong);

});

$('#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; }
const currentTime = player.getTime();
const duration = player.getDuration();
const percent = (currentTime / duration) * 100;
$('#time-control .current-time').text(player.prettyTime(currentTime));
$('#time-control .total-time').text(player.prettyTime(duration));
$('#time-control input').val(percent);
}, 1000);



$('button#previous').on('click', function() {
if (player.playState !== 'playing') { return; }

const currentSongIndex = album.songs.indexOf(player.currentlyPlaying);
const previousSongIndex = currentSongIndex - 1;
if (previousSongIndex < 0) { return; }

const previousSong = album.songs[previousSongIndex];
helper.playPauseAndUpdate(previousSong);

});

}
16 changes: 11 additions & 5 deletions scripts/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,17 +37,23 @@ 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) {
const minutes = Math.floor(timeInSeconds / 60);
const seconds = Math.floor(timeInSeconds % 60) <=9 ? `0${Math.floor(timeInSeconds % 60)}` : Math.floor(timeInSeconds % 60);
return `${minutes}:${seconds}`;
}

}

const player = new Player();

24 changes: 24 additions & 0 deletions scripts/song-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
album.songs.forEach( (song, index) => {
song.element= $(`
<tr>
<td>
<button>
<span class="song-number">${index + 1}</span>
<span class="ion-play"></span>
<span class="ion-pause"></span>
</button>
</td>
<td>${song.title}</td>
<td>${player.prettyTime(song.duration)}</td>
</tr>
`);

song.element.on('click', event => {
helper.playPauseAndUpdate(song);
$('button#play-pause').attr('playState', player.playState);
});

$('#song-list').append(song.element);
});
}