Commits

Mathias Panzenböck committed 349321d

connect to play control web socket server

Comments (0)

Files changed (2)

javascripts/magnatune.js

 		})(),
 		Notifications: !!(window.webkitNotifications || navigator.mozNotification),
 		ContextMenu: 'contextMenu' in document.documentElement && 'HTMLMenuItemElement' in window,
-		LocalStorage: typeof(localStorage) !== "undefined",
+		LocalStorage: !!window.localStorage,
 		DownloadAttribute: 'download' in document.createElement('a'),
-		BuggyContextMenu: $.browser.mozilla && $.browser.parsedVersion[0] === 20
+		BuggyContextMenu: !!$.browser.mozilla && $.browser.parsedVersion[0] === 20
 	},
 	Options: {
 		AnimationDuration: 500
 	getNotificationsEnabled: function () {
 		return getBoolean('notifications.enabled');
 	},
+	PlayControl: {
+		_sockets: [],
+		connect: function (host, port) {
+			var socket = new WebSocket("ws://localhost:8001/playctrl?"+$.param({
+				name: 'Greattune Player',
+				icon: location.protocol+'//'+location.host+'/app/greattune-player/logo_64.png',
+				capabilities: 'play pause seek go_next go_prev'
+			}));
+
+			socket.onopen = function () {
+				console.log("ws open");
+				Magnatune.PlayControl._sockets.push(socket);
+			};
+
+			socket.onmessage = function (event) {
+				console.log("message: "+event.data);
+				var message = JSON.parse(event.data);
+				switch (message.type) {
+					case "playPause":
+						Magnatune.Player.playPause();
+						break;
+
+					case "play":
+						// force play without reset
+						if (Magnatune.Player.paused()) {
+							Magnatune.Player.playPause();
+						}
+						else if (!Magnatune.Player.playing()) {
+							Magnatune.Player.play();
+						}
+						break;
+
+					case "pause":
+						Magnatune.Player.pause();
+						break;
+
+					case "stop":
+						Magnatune.Player.stop();
+						break;
+
+					case "quit":
+						if (!event.altKey) return;
+						window.open('', '_self', '');
+						window.close();
+						break;
+
+					case "next":
+						Magnatune.Playlist.next();
+						break;
+
+					case "prev":
+						Magnatune.Playlist.previous();
+						break;
+
+					case "backward":
+						Magnatune.Player.seekBackward();
+						break;
+
+					case "forward":
+						Magnatune.Player.seekForward();
+						break;
+
+				}
+			};
+
+			socket.onclose = function () {
+				console.log("ws close");
+				var i = Magnatune.PlayControl._sockets.indexOf(socket);
+				if (i >= 0) {
+					Magnatune.PlayControl._sockets.splice(i, 1);
+				}
+			};
+
+			socket.onerror = function (event) {
+				console.log("ws error",event);
+				var i = Magnatune.PlayControl._sockets.indexOf(socket);
+				if (i >= 0) {
+					Magnatune.PlayControl._sockets.splice(i, 1);
+				}
+			};
+		},
+		send: function (message) {
+			message = JSON.stringify(message);
+			for (var i = 0; i < Magnatune.PlayControl._sockets.length; ++ i) {
+				try {
+					Magnatune.PlayControl._sockets[i].send(message);
+				}
+				catch (e) {
+					console.error(e);
+				}
+			}
+		}
+	},
 	Player: {
 		_song: null,
 		song: function () {
 			if (song) {
 				var title = song.desc;
 				var album = Magnatune.Collection.Albums[song.albumname];
-
-				document.title = '\u25b6 '+title+' - '+album.artist.artist+' - Greattune Player';
-
-				if (// firefox fires this event on seeking!
-					(!this._lastSong || this._lastSong.mp3 !== song.mp3) &&
-					Magnatune.BrowserFeatures.LocalStorage &&
-					getBoolean('notifications.enabled') &&
-					((window.webkitNotifications &&
-					window.webkitNotifications.checkPermission() === 0) ||
-					navigator.mozNotification)) {
-					var iconUrl = 'http://he3.magnatune.com/music/'+
-						encodeURIComponent(album.artist.artist)+'/'+
-						encodeURIComponent(album.albumname)+'/cover_50.jpg';
-					var description = "by "+album.artist.artist+" from the album "+song.albumname;
-
-					var notification = navigator.mozNotification ?
-						navigator.mozNotification.createNotification(title, description, iconUrl) :
-						window.webkitNotifications.createNotification($.browser.mozilla ? absurl("/images/logo.png") : iconUrl, title, description);
-					notification.ondisplay = this._timed_hide_notification;
-					notification.show();
-					this._lastSong = song;
+				var artist = album.artist.artist;
+
+				document.title = '\u25b6 '+title+' - '+artist+' - Greattune Player';
+
+				if (wasplaying) {
+					Magnatune.PlayControl.send({type:'statuschange',status:'playing'});
+				}
+
+				// firefox fires this event on seeking!
+				if (!this._lastSong || this._lastSong.mp3 !== song.mp3) {
+					Magnatune.PlayControl.send({type:'trackchange',albumname:song.albumname,title:title,artist:artist});
+
+					if (Magnatune.BrowserFeatures.LocalStorage &&
+						getBoolean('notifications.enabled') &&
+						((window.webkitNotifications &&
+						window.webkitNotifications.checkPermission() === 0) ||
+						navigator.mozNotification)) {
+						var iconUrl = 'http://he3.magnatune.com/music/'+
+							encodeURIComponent(album.artist.artist)+'/'+
+							encodeURIComponent(album.albumname)+'/cover_50.jpg';
+						var description = "by "+album.artist.artist+" from the album "+song.albumname;
+
+						var notification = navigator.mozNotification ?
+							navigator.mozNotification.createNotification(title, description, iconUrl) :
+							window.webkitNotifications.createNotification($.browser.mozilla ? absurl("/images/logo.png") : iconUrl, title, description);
+						notification.ondisplay = this._timed_hide_notification;
+						notification.show();
+						this._lastSong = song;
+					}
 				}
 			}
 		},
 				}.bind(this), timeout);
 			}
 		},
+		_paused: function () {
+			Magnatune.PlayControl.send({type:'statuschange',status:'paused'});
+			Magnatune.Player._not_playing();
+		},
 		_not_playing: function () {
 			$('#play-text').show();
 			$('#pause-text').hide();
 			}
 		},
 		_ended: function () {
+			Magnatune.PlayControl.send({type:'statuschange',status:'stopped'});
 			this._not_playing();
 			Magnatune.Player.hideSpinner();
 			if (!Magnatune.DnD.seeking) {
 				Magnatune.Player.hideSpinner();
 			},
 			pause: function (event) {
-				Magnatune.Player._not_playing();
+				Magnatune.Player._paused();
 			},
 			ended: function (event) {
 				Magnatune.Player._ended();
 		}
 	}).on('hashchange',function (event) {
 		Magnatune.showHash();
-	}).on('keyup',function (event) {
-		// media key bindings
-		if (event.altKey || event.ctrlKey || event.metaKey) return;
-		switch (event.key) {
-			case "MediaPlay":
-			case "MediaPlayPause":
-				if (event.shiftKey) return;
-				Magnatune.Player.playPause();
-				break;
-				
-			case "MediaPause":
-				if (event.shiftKey) return;
-				Magnatune.Player.pause();
-				break;
-
-			case "MediaStop":
-				if (event.shiftKey) return;
-				Magnatune.Player.stop();
-				break;
-
-			case "U+00A6": if (!$.browser.webkit) return;
-			case "BrowserBack":
-			case "MediaTrackPrevious":
-			case "MediaPreviousTrack":
-				if (event.shiftKey) {
-					Magnatune.Player.seekBackward();
-				}
-				else {
-					Magnatune.Playlist.previous();
-				}
-				break;
-
-			case "U+00A7": if (!$.browser.webkit) return;
-			case "BrowserForward":
-			case "MediaTrackNext":
-			case "MediaNextTrack":
-				if (event.shiftKey) {
-					Magnatune.Player.seekForward();
-				}
-				else {
-					Magnatune.Playlist.next();
-				}
-				break;
-
-			case "BrowserSearch":
-				if (event.shiftKey) return;
-				$('#search').focus().select();
-				break;
-
-			// technically not correct.
-			// rewind/fast forward should change playback speed
-			case "MediaRewind":
-				if (event.shiftKey) return;
-				Magnatune.Player.seekBackward();
-				break;
-				
-			case "MediaFastForward":
-				if (event.shiftKey) return;
-				Magnatune.Player.seekForward();
-				break;
-
-			case "Close":
-				if (event.shiftKey) return;
-				window.open('', '_self', '');
-				window.close();
-				break;
-
-			default:
-				return;
-		}
-		event.preventDefault();
-		event.stopPropagation();
 	});
 
 	if (typeof(FileReader) === "undefined") {
 			};
 		}
 	});
+
+	Magnatune.PlayControl.connect("localhost",8001);
 });
 
 // If installed as Chrome app window.moveTo and window.resizeTo are not blocked
 {
   "name": "GreattunePlayer",
   "subdomain": "greattuneplayer",
-  "version": "0.1.0-200",
+  "version": "0.1.0-203",
   "author": "Mathias Panzenböck",
   "description": "This is a HTML5 audio player based on the music from Magnatune.com",
   "bin": {