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

add shuffle #61

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module.exports = function (grunt) {
'src/18-directive-playAll.js',
'src/19-directive-volumeBar.js',
'src/20-directive-playPauseToggle.js',
'src/21-directive-shuffleMusic.js',
'src/22-directive-shuffleAllMusic.js'
],
dest: 'dist/angular-soundmanager2.js'
}
Expand Down
98 changes: 93 additions & 5 deletions dist/angular-soundmanager2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4422,6 +4422,8 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',

var currentTrack = null,
repeat = false,
shuffle = false,
tempTrack = [],
autoPlay = true,
isPlaying = false,
volume = 90,
Expand Down Expand Up @@ -4620,6 +4622,7 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
}
//unload from soundManager
soundManager.destroySound(song);

//remove from playlist
playlist.splice(index, 1);
//once all done then broadcast
Expand Down Expand Up @@ -4678,16 +4681,30 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
$log.debug("Please click on Play before this action");
return null;
}
var currentTrackKey = this.getIndexByValue(soundManager.soundIDs, this.getCurrentTrack());

// use shuffle track list if shuffle is true
var useTrack = angular.copy(soundManager.soundIDs);
if(shuffle === true){
useTrack = tempTrack;
}

var currentTrackKey = this.getIndexByValue(useTrack, this.getCurrentTrack());
var nextTrackKey = +currentTrackKey + 1;
var nextTrack = soundManager.soundIDs[nextTrackKey];
var nextTrack = useTrack[nextTrackKey];
if(typeof nextTrack !== 'undefined') {
this.playTrack(nextTrack);
} else {
// generate shuffle track list
if(shuffle === true && isPlaying === true){
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
$rootScope.$broadcast('music:tempTrack', tempTrack);
}

//if no next track found
if(repeat === true) {
//start first track if repeat is on
this.playTrack(soundManager.soundIDs[0]);
this.playTrack(useTrack[0]);
} else {
//breadcase not playing anything
isPlaying = false;
Expand All @@ -4700,9 +4717,16 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
$log.debug("Please click on Play before this action");
return null;
}
var currentTrackKey = this.getIndexByValue(soundManager.soundIDs, this.getCurrentTrack());

// use shuffle track list if shuffle is true
var useTrack = angular.copy(soundManager.soundIDs);
if(shuffle === true){
useTrack = tempTrack;
}

var currentTrackKey = this.getIndexByValue(useTrack, this.getCurrentTrack());
var prevTrackKey = +currentTrackKey - 1;
var prevTrack = soundManager.soundIDs[prevTrackKey];
var prevTrack = useTrack[prevTrackKey];
if(typeof prevTrack !== 'undefined') {
this.playTrack(prevTrack);
} else {
Expand Down Expand Up @@ -4731,6 +4755,36 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
getRepeatStatus: function() {
return repeat;
},
shuffleToggle: function() {
if(shuffle === true) {
shuffle = false;
tempTrack = angular.copy(soundManager.soundIDs);
} else {
shuffle = true;
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
}
$rootScope.$broadcast('music:shuffle', shuffle);
$rootScope.$broadcast('music:tempTrack', tempTrack);
},
getShuffleStatus: function() {
return shuffle;
},
playShuffle: function(){
var trackToPlay = null;
shuffle = true;
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
$rootScope.$broadcast('music:shuffle', shuffle);
$rootScope.$broadcast('music:tempTrack', tempTrack);

if(tempTrack.length === 0) {
$log.debug('playlist is empty!');
return;
}
trackToPlay = tempTrack[0];
this.initPlayTrack(trackToPlay);
},
getVolume: function() {
return volume;
},
Expand Down Expand Up @@ -5173,3 +5227,37 @@ ngSoundManager.directive('playPauseToggle', ['angularPlayer',
};
}
]);


ngSoundManager.directive('shuffleMusic', ['angularPlayer', function (angularPlayer) {
return {
restrict: "EA",
link: function (scope, element, attrs) {

element.bind('click', function (event) {
angularPlayer.shuffleToggle();
});

scope.shuffle = angularPlayer.getShuffleStatus();
scope.$on('music:shuffle', function (event, data) {
scope.$apply(function () {
scope.shuffle = data;
});
});
}
};
}]);

ngSoundManager.directive('shuffleAllMusic', ['angularPlayer', function (angularPlayer) {
return {
restrict: "EA",
link: function (scope, element, attrs) {

element.bind('click', function (event) {
angularPlayer.playShuffle();
});

scope.shuffle = angularPlayer.getShuffleStatus();
}
};
}]);
6 changes: 3 additions & 3 deletions dist/angular-soundmanager2.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ <h5>Songs</h5>
-
<button repeat-music>Repeat ({{ repeat }})</button>
-
<button shuffle-music>Shuffle ({{ shuffle }})</button>
-
<button shuffle-all-music>Play Shuffle ({{ shuffle }})</button>
-
<button play-pause-toggle data-play="Play!" data-pause="Pause!">Play Toggle</button>

Is Playing: {{ isPlaying }}
Expand Down
64 changes: 59 additions & 5 deletions src/03-factory-angularPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',

var currentTrack = null,
repeat = false,
shuffle = false,
tempTrack = [],
autoPlay = true,
isPlaying = false,
volume = 90,
Expand Down Expand Up @@ -201,6 +203,7 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
}
//unload from soundManager
soundManager.destroySound(song);

//remove from playlist
playlist.splice(index, 1);
//once all done then broadcast
Expand Down Expand Up @@ -259,16 +262,30 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
$log.debug("Please click on Play before this action");
return null;
}
var currentTrackKey = this.getIndexByValue(soundManager.soundIDs, this.getCurrentTrack());

// use shuffle track list if shuffle is true
var useTrack = angular.copy(soundManager.soundIDs);
if(shuffle === true){
useTrack = tempTrack;
}

var currentTrackKey = this.getIndexByValue(useTrack, this.getCurrentTrack());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

var nextTrackKey = +currentTrackKey + 1;
var nextTrack = soundManager.soundIDs[nextTrackKey];
var nextTrack = useTrack[nextTrackKey];
if(typeof nextTrack !== 'undefined') {
this.playTrack(nextTrack);
} else {
// generate shuffle track list
if(shuffle === true && isPlaying === true){
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

$rootScope.$broadcast('music:tempTrack', tempTrack);
}

//if no next track found
if(repeat === true) {
//start first track if repeat is on
this.playTrack(soundManager.soundIDs[0]);
this.playTrack(useTrack[0]);
} else {
//breadcase not playing anything
isPlaying = false;
Expand All @@ -281,9 +298,16 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
$log.debug("Please click on Play before this action");
return null;
}
var currentTrackKey = this.getIndexByValue(soundManager.soundIDs, this.getCurrentTrack());

// use shuffle track list if shuffle is true
var useTrack = angular.copy(soundManager.soundIDs);
if(shuffle === true){
useTrack = tempTrack;
}

var currentTrackKey = this.getIndexByValue(useTrack, this.getCurrentTrack());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

var prevTrackKey = +currentTrackKey - 1;
var prevTrack = soundManager.soundIDs[prevTrackKey];
var prevTrack = useTrack[prevTrackKey];
if(typeof prevTrack !== 'undefined') {
this.playTrack(prevTrack);
} else {
Expand Down Expand Up @@ -312,6 +336,36 @@ ngSoundManager.factory('angularPlayer', ['$rootScope', '$log',
getRepeatStatus: function() {
return repeat;
},
shuffleToggle: function() {
if(shuffle === true) {
shuffle = false;
tempTrack = angular.copy(soundManager.soundIDs);
} else {
shuffle = true;
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

}
$rootScope.$broadcast('music:shuffle', shuffle);
$rootScope.$broadcast('music:tempTrack', tempTrack);
},
getShuffleStatus: function() {
return shuffle;
},
playShuffle: function(){
var trackToPlay = null;
shuffle = true;
tempTrack = angular.copy(soundManager.soundIDs);
tempTrack = (tempTrack).sort(function() { return 0.5 - Math.random(); });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

$rootScope.$broadcast('music:shuffle', shuffle);
$rootScope.$broadcast('music:tempTrack', tempTrack);

if(tempTrack.length === 0) {
$log.debug('playlist is empty!');
return;
}
trackToPlay = tempTrack[0];
this.initPlayTrack(trackToPlay);
},
getVolume: function() {
return volume;
},
Expand Down
18 changes: 18 additions & 0 deletions src/21-directive-shuffleMusic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ngSoundManager.directive('shuffleMusic', ['angularPlayer', function (angularPlayer) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
'ngSoundManager' is not defined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
'ngSoundManager' is not defined.

return {
restrict: "EA",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

link: function (scope, element, attrs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'attrs' is defined but never used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'attrs' is defined but never used.


element.bind('click', function (event) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'event' is defined but never used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'event' is defined but never used.

angularPlayer.shuffleToggle();
});

scope.shuffle = angularPlayer.getShuffleStatus();
scope.$on('music:shuffle', function (event, data) {
scope.$apply(function () {
scope.shuffle = data;
});
});
}
};
}]);
13 changes: 13 additions & 0 deletions src/22-directive-shuffleAllMusic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ngSoundManager.directive('shuffleAllMusic', ['angularPlayer', function (angularPlayer) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
'ngSoundManager' is not defined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.
'ngSoundManager' is not defined.

return {
restrict: "EA",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed double and single quotes.

link: function (scope, element, attrs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'attrs' is defined but never used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'attrs' is defined but never used.


element.bind('click', function (event) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'event' is defined but never used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'event' is defined but never used.

angularPlayer.playShuffle();
});

scope.shuffle = angularPlayer.getShuffleStatus();
}
};
}]);