Skip to content

Instantly share code, notes, and snippets.

@gerzhan
Forked from ry8806/jplayer-directive.js
Last active November 11, 2015 05:17
Show Gist options
  • Save gerzhan/6b24a94ec59f452b1992 to your computer and use it in GitHub Desktop.
Save gerzhan/6b24a94ec59f452b1992 to your computer and use it in GitHub Desktop.
myApp.directive("jplayer", ['$window', 'PlayerService', function ($window, PlayerService) {
return {
restrict: "E",
// Have our own scope - we only want to watch the service and not conflict with other scopes
scope: {},
// Serve up some html with our player
templateUrl: "/jplayer-template.html",
link: function (scope, element, attrs) {
// An element on the page to attach the jPlayer to. Could also use "element" from linkFN ^
var jPlayer = angular.element("#jquery_jplayer_1").jPlayer();
// Point to the css
$window.jplayerCss = "#jp_container_1";
// Set up a playlist
$window.myPlaylist = new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: jplayerCss,
repeat: function (e) {
// Implement repeat from the service
}
}, [], { supplied: 'mp3', swfPath: "obj/", free: true }, 0);
// Add the player service to the scope so we can watch stuff!
scope.PlayerService = PlayerService;
// When the Current track (on the service) changes - we want to tell jPlayer to play that new song
scope.$watch('PlayerService.CurrentTrack', function (value) {
if (value != null) {
jPlayer.jPlayer('setMedia', {
// The url of the mp3 file
mp3: value.StreamUri,
title: value.Title
}).jPlayer('play');
}
});
// Pausing is controlled from a service (so many things can toggle it).
// So watch it an control jPlayer when it changes
scope.$watch('PlayerService.IsPaused', function (value) {
if (value != null) {
if (value == true) {
jPlayer.jPlayer('pause');
}
else {
jPlayer.jPlayer('play');
}
}
});
jPlayer.bind($.jPlayer.event.ended, function (event) {
// Song has ended, try to go next
if (scope.PlayerService.HasNext) {
scope.PlayerService.Next();
}
});
scope.$on('$destroy', function () {
// Clean up memory from the events
jPlayer.unbind($.jPlayer.event.ended);
jPlayer.unbind($.jplayer.event.play);
// Don't think we'll ever destroy it (it's on every page) - and why would you want to?
jPlayer.jPlayer('destroy');
});
}
};
}]);
<div id="jquery_jplayer_1" class="jp-jplayer"></div>
<div id="jp_container_1" class="jp-audio" role="application" aria-label="media player">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<div class="jp-volume-controls">
<button class="jp-mute" role="button" tabindex="0">mute</button>
<button class="jp-volume-max" role="button" tabindex="0">max volume</button>
<div class="jp-volume-bar">
<div class="jp-volume-bar-value"></div>
</div>
</div>
<div class="jp-controls-holder">
<div class="jp-controls">
<button class="jp-play" role="button" tabindex="0">play</button>
<button class="jp-stop" role="button" tabindex="0">stop</button>
</div>
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
<div class="jp-current-time" role="timer" aria-label="time">&nbsp;</div>
<div class="jp-duration" role="timer" aria-label="duration">&nbsp;</div>
<div class="jp-toggles">
<button class="jp-repeat" role="button" tabindex="0">repeat</button>
</div>
</div>
</div>
<div class="jp-details">
<div class="jp-title" aria-label="title">&nbsp;</div>
</div>
<div class="jp-no-solution">
<span>Update Required</span>
To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment