perminder-klair / angular-soundmanager2

SoundManager2 Music Player for AngularJs
http://perminder-klair.github.io/angular-soundmanager2/
MIT License
169 stars 108 forks source link

How to loop entire playlist? #45

Closed rochapablo closed 8 years ago

rochapablo commented 8 years ago

I'm adding the songs to player like this:

angularPlayer.clearPlaylist(function() {
    angular.forEach(serverItems, function(item) {
      angularPlayer.addTrack(item);
    });
    angularPlayer.play();
});

But at the end, after all played how to restart the playlist and keep it in loop?

rochapablo commented 8 years ago

Well, never mind!

Here's what I did:

$rootScope.$on('music:isPlaying', function(event, data) {

  var currentTrackKey = angularPlayer.getIndexByValue(soundManager.soundIDs, angularPlayer.getCurrentTrack());
  var nextTrackKey =+ currentTrackKey + 1;
  var nextTrack = soundManager.soundIDs[nextTrackKey];

  if(data === false && typeof nextTrack === 'undefined') {
    angularPlayer.initPlayTrack(soundManager.soundIDs[0], true);
  }

});
oliwin commented 7 years ago

As solution I suggest:

$scope.play = function (genre) {

    $timeout(function () {

        angularPlayer.stop();
        angularPlayer.setCurrentTrack(null);

        angularPlayer.clearPlaylist(function () {

            if (genre !== undefined) {
                $scope.filtered = filterFilter($scope.songs, {'genre': genre});

            } else {

                $scope.filtered = $scope.songs;
            }

            if (random) {
                $scope.filtered = $filter('shuffleArray')($scope.filtered);
            }

            if ($scope.filtered.length == 0) {
                console.log("No songs by genre " + genre);
            }

            angular.forEach($scope.filtered, function (value) {
                angularPlayer.addTrack(value);
            });

            angularPlayer.play();

        });

    });
};
rochapablo commented 7 years ago

Hey @oliwin,

I tried fix the loop and it's seems that now it's working.

You can check the code bellow or the example running.

app.controller('BaseCtrl', ['$rootScope', '$timeout', 'angularPlayer', function($root, $timeout, player) {

  var $ctrl = this;

  $ctrl.songs = dbsongs();

  // here, i'm also following the pattern of the library
  var nextTrack = function() {
    var current = player.getIndexByValue(soundManager.soundIDs, player.getCurrentTrack());
    var index = +current + 1;
    return soundManager.soundIDs[index];
  }

  $root.$on('track:progress', function(event, data) {

    // at the end of the track 100% will be given
    if (data < 100) {
      return;
    }

    // undefined means... well nothing more to play
    if (typeof nextTrack() !== 'undefined') {
      return;
    }

    // not sure yet, but... sometimes an error will apper ".length undefined"
    // $timeout(function() {

    // here, i'm just follow the pattern of the angular-soundmanager2.js
    player.clearPlaylist(function(data) {
      player.setCurrentTrack(null);
      player.stop();
      // as the playlist was cleaned, we need to add all over again
      for (var i = 0; i < $ctrl.songs.length; i++) {
        player.addTrack($ctrl.songs[i]);
      }
      player.play();
    });

    // }, 1001);

  });

}]);

Hope that now we had kill these dragon.

Peace man!

oliwin commented 7 years ago

Hi, man! It is great, but I can not see play() method, how to start playing?

2017-02-10 2:01 GMT+04:00 Pablo notifications@github.com:

Hey @oliwin https://github.com/oliwin,

I tried fix the loop and it's seems that now it's working.

You can check the code bellow or the example running http://codepen.io/rochapablo/pen/jyedeE?editors=0011.

app.controller('BaseCtrl', ['$rootScope', '$timeout', 'angularPlayer', function($root, $timeout, player) {

var $ctrl = this;

$ctrl.songs = dbsongs();

// here, i'm also following the pattern of the library var nextTrack = function() { var current = player.getIndexByValue(soundManager.soundIDs, player.getCurrentTrack()); var index = +current + 1; return soundManager.soundIDs[index]; }

$root.$on('track:progress', function(event, data) {

// at the end of the track 100% will be given
if (data < 100) {
  return;
}

// undefined means... well nothing more to play
if (typeof nextTrack() !== 'undefined') {
  return;
}

// not sure yet, but... sometimes an error will apper ".length undefined"
// $timeout(function() {

// here, i'm just follow the pattern of the angular-soundmanager2.js
player.clearPlaylist(function(data) {
  player.setCurrentTrack(null);
  player.stop();
  // as the playlist was cleaned, we need to add all over again
  for (var i = 0; i < $ctrl.songs.length; i++) {
    player.addTrack($ctrl.songs[i]);
  }
  player.play();
});

// }, 1001);

});

}]);

Hope that now we had kill these dragon.

Peace man!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/perminder-klair/angular-soundmanager2/issues/45#issuecomment-278788425, or mute the thread https://github.com/notifications/unsubscribe-auth/AE4BpoZ309N23dBuvLsqqa7UszkHhsglks5ra4zOgaJpZM4HyDmF .

-- Олег Валерьевич Телефон: +(994) 51 40 333 90

rochapablo commented 7 years ago

@oliwin,

The play() was a method that I was using in another way, you can keep using, but you'll have to implement that.

The main problem was showing the current name, which we change frommusic:isPlaying to `'track:progress' plus some code inside.

I'm playing all the songs using the play-all directive.

I don't know if you read, but for me this link had helped me a lot.

Good luck