Skip to content

Commit ad29713

Browse files
committed
Change how play none, one and all feature works.
1 parent b2840fc commit ad29713

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Then add the song list to the `init` method:
1717

1818
```javascript
1919
player.init({
20+
loop: 'none', // 'none', 'one', 'all'. Default: 'none'.
2021
songs: [
2122
//{url: 'https://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample', name: 'Black in Black'},
2223
{url: 'songs/one', name: 'Querendo Chorar'},
@@ -27,6 +28,14 @@ player.init({
2728
});
2829
```
2930

31+
The `loop` option works as follows:
32+
33+
- `none`: play up to the last song and stop (don't start over from the beginning
34+
of the playlist).
35+
- `one`: keep playing the same song forever.
36+
- `all`: after playing the last song, start again from the first item in the
37+
playlist.
38+
3039

3140
## Code Style
3241

init.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

player.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,13 @@ var player = (function () {
354354
//
355355
_self.currentSongIndex = parseInt(this.getAttribute('data-index'), 10);
356356

357-
changeSong(_self.currentSongIndex);
357+
playSong(_self.currentSongIndex);
358358
});
359359
});
360360

361361
}
362362

363-
function changeSong(index) {
363+
function playSong(index) {
364364

365365
_self.currentSongIndex = index;
366366

@@ -375,18 +375,27 @@ var player = (function () {
375375

376376
function nextSong() {
377377

378-
if (_self.config.loop === 'none') return;
379-
378+
// No mater if playlist should replay from start or not, if `loop` is `one`,
379+
// keep playing the same song. `currentSongIndex` does not change.
380380
if (_self.config.loop === 'one') {
381-
changeSong(_self.currentSongIndex);
381+
playSong(_self.currentSongIndex);
382382
return;
383383
}
384384

385+
// If we are at the last song and `loop` is `none`, DO NOT start over playing at
386+
// the first item in the playlist.
387+
if (_self.currentSongIndex === _self.config.songs.length - 1 && _self.config.loop === 'none') {
388+
return;
389+
}
390+
391+
392+
// If we got to this point, either play the next song or start over again
393+
// from the first item in the playlist.
385394
var songIndex = (_self.currentSongIndex < _self.config.songs.length - 1)
386395
? songIndex = _self.currentSongIndex + 1
387396
: songIndex = 0;
388397

389-
changeSong(songIndex);
398+
playSong(songIndex);
390399
}
391400

392401
function bindEventHandlers() {

0 commit comments

Comments
 (0)