Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ <h4 id="playerUser" class="player_user" ng-click="goToUser($event)"></h4>
<script src="public/js/common/SCapiService.js"></script>
<script src="public/js/common/SC2apiService.js"></script>
<script src="public/js/common/playerService.js"></script>
<script src="public/js/common/mprisService.js"></script>
<script src="public/js/common/notificationFactory.js"></script>
<script src="public/js/common/modalFactory.js"></script>

Expand Down
86 changes: 86 additions & 0 deletions app/public/js/common/mprisService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";

var gui = require("nw.gui");

/**
* mpris integration for linux systems using DBUS
*/
app.factory("mprisService", function($rootScope, $log, $timeout, $window, $state) {
// media keys are supported on osx/windows already anyway
var supportedPlatforms = {
"linux": true,
"win32": false,
"darwin": false
};

// We check if the platform is supported
if(!supportedPlatforms[process.platform]) return false;

// Require mpris
var Player = require("mpris-service");

// Initialize & configure mpris
var mprisPlayer = Player({
canRaise: true,
name: "soundnode",
identity: "Soundnode",
desktopEntry: "soundnode",
supportedInterfaces: ["player"],
supportedUriSchemes: ["http", "file"],
supportedMimeTypes: ["audio/mpeg", "application/ogg"]
});

// Configuration
mprisPlayer.canControl = true;
mprisPlayer.canSeek = false;

// When the user asks dbus to show the player, we'll show it.
mprisPlayer.on("raise", function() {
gui.Window.get().show();
});

/** Functions **/

/**
* This is called whenever you play/resume a track.
*
* @param trackid {number} (SC Track ID)
* @param length {number} (Microseconds - Integer)
* @param artwork {uri}
* @param title {string}
* @param artist {string}
*/
mprisPlayer.play = function(trackid, length, artwork, title, artist) {
/** Overrides **/
length = 0; // UNSUPPORTED

mprisPlayer.metadata = {
"mpris:trackid": mprisPlayer.objectPath("track/" + trackid),
"mpris:length": length,
"mpris:artUrl": artwork,
"xesam:title": title,
"xesam:album": "",
"xesam:artist": artist
};

// Tell dbus/mpris that we're currently playing.
mprisPlayer.playbackStatus = "Playing";
};

/**
* Tells mpris that we're paused.
*/
mprisPlayer.pause = function() {
mprisPlayer.playbackStatus = "Paused";
};

/**
* This is called whenever you stop playback
*/
mprisPlayer.stop = function() {
mprisPlayer.playbackStatus = "Stopped";
};

// Return the mprisPlayer object
return mprisPlayer;
});
44 changes: 44 additions & 0 deletions app/public/js/common/playerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ app.factory('playerService', function(
$timeout,
$window,
$state,
mprisService,
notificationFactory,
queueService,
utilsService
Expand Down Expand Up @@ -137,6 +138,7 @@ app.factory('playerService', function(
player.playNewSong = function() {
var trackObj = queueService.getTrack();
var trackObjId = trackObj.songId;
var duration = player.elPlayer.duration * 1000;
var songNotification;

utilsService.deactivateCurrentSong();
Expand Down Expand Up @@ -181,6 +183,12 @@ app.factory('playerService', function(
// remove the active class from player favorite icon before play new song
// TODO: this should check if the current song is already favorited
document.querySelector('.player_favorite').classList.remove('active');

// mpris only supports linux
if(process.platform === "linux") {
// tell mpris that we're now playing & send off the attributes for dbus to use.
mprisService.play("0", duration, trackObj.songThumbnail, trackObj.songTitle, trackObj.songUser);
}
};

// Updates cache when liking or unliking a song, so future checks will be correct
Expand All @@ -196,8 +204,17 @@ app.factory('playerService', function(
* @method playSong
*/
player.playSong = function() {
var duration = player.elPlayer.duration * 1000;

this.elPlayer.play();
$rootScope.isSongPlaying = true;

/**
* linux mpris passthrough for media keys & desktop integration
*/
if(process.platform === "linux") {
mprisService.play("0", duration, player.elThumb.src, player.elTitle.innerHTML, player.elUser.innerHTML);
}
};

/**
Expand All @@ -207,6 +224,24 @@ app.factory('playerService', function(
player.pauseSong = function() {
this.elPlayer.pause();
$rootScope.isSongPlaying = false;

/**
* linux mpris passthrough for media keys & desktop integration
*/
if(process.platform === "linux") {
mprisService.pause();
}
};

/**
* Sets the songs time to 0 & then pauses the song, stopping the song.
*/
player.stopSong = function() {
// Set time to 0
player.setSongTime(0);

// Set a paused state
player.pauseSong();
};

/**
Expand Down Expand Up @@ -256,6 +291,15 @@ app.factory('playerService', function(
this.playNewSong();
};

/**
* Set the song time.
* @param {Number} time The time in milliseconds you want to set
*/
player.setSongTime = function(time) {
if(isNaN(time)) throw new Error("You can only set time with a number");
return document.getElementById('player').currentTime = time;
}

/**
* Add event listener "on ended" to player
*/
Expand Down
67 changes: 50 additions & 17 deletions app/public/js/player/playerCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ app.controller('PlayerCtrl', function (
$scope,
$rootScope,
playerService,
mprisService,
queueService,
hotkeys,
$state,
Expand Down Expand Up @@ -147,20 +148,27 @@ app.controller('PlayerCtrl', function (
}
});

/**
* Used between multiple functions, so we'll leave it here so it reduces
* the amount of times we define it.
*/
var togglePlayPause = function() {
if ( $rootScope.isSongPlaying ) {
playerService.pauseSong();
} else {
playerService.playSong();
}
};


/*
* Add native media shortcuts
*/

var playPause = new gui.Shortcut({
key: 'MediaPlayPause',
active: function() {
$scope.$apply(function() {
if ( $rootScope.isSongPlaying ) {
playerService.pauseSong();
} else {
playerService.playSong();
}
togglePlayPause();
});
},
failed: function() {
Expand All @@ -173,7 +181,7 @@ app.controller('PlayerCtrl', function (
active: function() {
$scope.$apply(function() {
if ( $rootScope.isSongPlaying ) {
playerService.pauseSong();
playerService.stopSong();
}
});
},
Expand Down Expand Up @@ -215,6 +223,39 @@ app.controller('PlayerCtrl', function (
gui.App.registerGlobalHotKey(prevTrack);
gui.App.registerGlobalHotKey(nextTrack);

/**
* Add native media shortcuts for linux based systems
*/
if(process.platform === "linux") {
// Set a default state
mprisService.playbackStatus = mprisService.playbackStatus || "Stopped";

// When the user toggles play/pause
mprisService.on("playpause", function() {
togglePlayPause();
});

// When the user stops the song
mprisService.on("stop", function() {
playerService.stopSong();
});

// When the user requests next song
mprisService.on("next", function() {
playerService.playNextSong();
});

// When the user requests previous song
mprisService.on("previous", function() {
playerService.playPrevSong();
});

// When the user toggles shuffle
mprisService.on("shuffle", function() {
playerService.shuffle();
});
}

// function unregister() {
// gui.App.unregisterGlobalHotKey(shortcut);
// }
Expand All @@ -228,23 +269,15 @@ app.controller('PlayerCtrl', function (
description: 'Play/Pause song',
callback: function(event) {
event.preventDefault();
if ( $rootScope.isSongPlaying ) {
playerService.pauseSong();
} else {
playerService.playSong();
}
togglePlayPause();
}
});

hotkeys.add({
combo: ['command+return', 'ctrl+return'],
description: 'Play/Pause song',
callback: function() {
if ( $rootScope.isSongPlaying ) {
playerService.pauseSong();
} else {
playerService.playSong();
}
togglePlayPause();
}
});

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"time-grunt": "^1.2.2",
"webpack": "^1.12.2"
},
"optionalDependencies": {
"dbus": "MarshallOfSound/node-dbus#linux-only",
"mpris-service": "MarshallOfSound/mpris-service"
},
"dependencies": {
"react": "^0.14.2",
"react-dom": "^0.14.2",
Expand Down
3 changes: 2 additions & 1 deletion tasks/nwjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var config = {
'<%= settings.app %>/public/img/**/*',
'<%= settings.app %>/public/stylesheets/css/**/*',
'<%= settings.app %>/dist/**/*',
'./node_modules/universal-analytics/**/*'
'./node_modules/universal-analytics/**/*',
'./node_modules/mpris-service/**/*'
]
};

Expand Down
10 changes: 8 additions & 2 deletions tasks/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module.exports = {
stdout: true
},
target: {
command: 'webpack -p'
command: function() {
if("linux" === process.platform) {
return 'webpack -p && export PYTHON=/usr/bin/python2 && cd ./node_modules/mpris-service/node_modules/dbus && nw-gyp rebuild --target=0.12.3';
} else {
return 'webpack -p';
}
}
}
};
};