Skip to content

Commit

Permalink
33. jQuery: Buzz Library
Browse files Browse the repository at this point in the history
  • Loading branch information
Colleen committed Sep 29, 2015
1 parent 16d1562 commit 9f33206
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions album.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ <h3 class="artist-name"></h3>
</div>
</section>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/buzz/1.1.10/buzz.min.js"></script>
<script src="scripts/fixtures.js"></script>
<script src="scripts/album.js"></script>
</body>
Expand Down
33 changes: 29 additions & 4 deletions scripts/album.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ var createSongRow = function(songNumber, songName, songLength) {
if (currentlyPlayingSongNumber !== songNumber) {
$(this).html(pauseButtonTemplate);
setSong(songNumber);
currentSoundFile.play();
currentSongFromAlbum = currentAlbum.songs[songNumber - 1];
updatePlayerBarSong();
} else if (currentlyPlayingSongNumber === songNumber) {
$(this).html(playButtonTemplate);
$('.main-controls .play-pause').html(playerBarPlayButton);
currentlyPlayingSongNumber = null;
currentSongFromAlbum = null;
if (currentSoundFile.isPaused()) {
$(this).html(pauseButtonTemplate);
$('.main-controls .play-pause').html(playerBarPauseButton);
currentSoundFile.play();
} else {
$(this).html(playButtonTemplate);
$('.main-controls .play-pause').html(playerBarPlayButton);
currentSoundFile.pause();
}
}
};

Expand Down Expand Up @@ -57,8 +63,23 @@ var createSongRow = function(songNumber, songName, songLength) {
};

var setSong = function(songNumber) {
if (currentSoundFile) {
currentSoundFile.stop();
}

currentlyPlayingSongNumber = parseInt(songNumber);
currentSongFromAlbum = currentAlbum.songs[songNumber - 1];
currentSoundFile = new buzz.sound(currentSongFromAlbum.audioUrl, {
formats: [ 'mp3' ],
preload: true
});
setVolume(currentVolume);
};

var setVolume = function(volume) {
if (currentSoundFile) {
currentSoundFile.setVolume(volume);
}
};

var setCurrentAlbum = function(album) {
Expand Down Expand Up @@ -108,6 +129,7 @@ var nextSong = function() {
}

setSong(currentSongIndex + 1);
currentSoundFile.play();
currentSongFromAlbum = currentAlbum.songs[currentSongIndex];

$('.currently-playing .song-name').text(currentSongFromAlbum.name);
Expand Down Expand Up @@ -136,6 +158,7 @@ var previousSong = function() {
}

setSong(currentSongIndex + 1);
currentSoundFile.play();
currentSongFromAlbum = currentAlbum.songs[currentSongIndex];

$('.currently-playing .song-name').text(currentSongFromAlbum.name);
Expand All @@ -159,6 +182,8 @@ var playerBarPauseButton = '<span class="ion-pause"></span>';
var currentAlbum = null;
var currentlyPlayingSongNumber = null;
var currentSongFromAlbum = null;
var currentSoundFile = null;
var currentVolume = 80;

var $previousButton = $('.main-controls .previous');
var $nextButton = $('.main-controls .next');
Expand Down
10 changes: 5 additions & 5 deletions scripts/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ var albumPicasso = {
year: '1881',
albumArtUrl: 'assets/images/album_covers/01.png',
songs: [
{ name: 'Blue', length: '4:26' },
{ name: 'Green', length: '3:14' },
{ name: 'Red', length: '5:01' },
{ name: 'Pink', length: '3:21' },
{ name: 'Magenta', length: '2:15' }
{ name: 'Blue', length: '4:26', audioUrl: 'assets/music/blue' },
{ name: 'Green', length: '3:14', audioUrl: 'assets/music/green' },
{ name: 'Red', length: '5:01', audioUrl: 'assets/music/red' },
{ name: 'Pink', length: '3:21', audioUrl: 'assets/music/pink' },
{ name: 'Magenta', length: '2:15', audioUrl: 'assets/music/magenta' }
]
};

Expand Down

0 comments on commit 9f33206

Please sign in to comment.