Permalink
Browse files

adding full support for the spotify UI in Muse Control. WIP

  • Loading branch information...
1 parent 2b1b99d commit 6538f7e3f4fa13175124f280abf53003f877eac2 John Watson committed Apr 1, 2012
View
@@ -3,8 +3,8 @@
<component name="FacetManager">
<facet type="scala" name="Scala">
<configuration>
- <option name="compilerLibraryLevel" value="Global" />
- <option name="compilerLibraryName" value="scala-compiler-2.9" />
+ <option name="compilerLibraryLevel" value="Project" />
+ <option name="compilerLibraryName" value="scala-compiler" />
<option name="deprecationWarnings" value="true" />
<option name="explainTypeErrors" value="true" />
<option name="optimiseBytecode" value="true" />
@@ -4,6 +4,7 @@
import nl.pascaldevink.jotify.gui.listeners.PlayerListener;
import java.awt.*;
+import java.net.URL;
import java.util.List;
public interface NativeSpotifySupport extends MusicPlayer {
@@ -25,5 +26,7 @@
Image image(String imageCode);
+ URL imageUrl(String imageCode);
+
void setVolume(float volume);
}
@@ -15,6 +15,8 @@
import javax.swing.*;
import java.awt.*;
import java.io.*;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -281,6 +283,16 @@ public Image image(String imageCode) {
}
@Override
+ public URL imageUrl(String imageCode) {
+ try {
+ return new URL("http://o.scdn.co/image/" + imageCode);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ @Override
public void bounce() {
//no op for now...
}
@@ -301,7 +313,7 @@ public MusicPlayerState getState() {
artistName = currentTrack.getArtist().getName();
albumName = currentTrack.getAlbum().getName();
//todo this is not a URL that anyone can use to get the image... figure out how to get an image URL from spotify, if it is even possible.
- cover = currentTrack.getCover();
+ cover = imageUrl(currentTrack.getCover()).toString();
}
String playlistName = "";
if (currentPlaylist != null) {
@@ -2,11 +2,17 @@
import com.google.gson.Gson;
+import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.awt.image.BufferedImage;
+import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
public class SpotifyServlet extends HttpServlet {
@@ -29,20 +35,47 @@ protected void doGet(HttpServletRequest req, HttpServletResponse response) throw
if (pathInfo.startsWith("/playpause")) {
spotifySupport.playPause();
- }
- else if (pathInfo.startsWith("/previous")) {
+ } else if (pathInfo.startsWith("/previous")) {
spotifySupport.previous();
- }
- else if (pathInfo.startsWith("/next")) {
+ } else if (pathInfo.startsWith("/next")) {
spotifySupport.next();
+ } else if (pathInfo.startsWith("/artwork")) {
+ File tempFile = File.createTempFile("spotify", ".tiff");
+ tempFile.deleteOnExit();
+ appleScriptTemplate.execute(Application.SPOTIFY, getImageCommand(tempFile));
+ BufferedImage image = ImageIO.read(tempFile);
+ response.setContentType("image/png");
+ ImageIO.write(image, "PNG", response.getOutputStream());
+ response.setStatus(HttpServletResponse.SC_OK);
+ return;
}
-
response.setStatus(HttpServletResponse.SC_OK);
appendStatus(response);
}
-
private void appendStatus(HttpServletResponse response) throws IOException {
response.getWriter().append(new Gson().toJson(spotifySupport.getStatus()));
}
+
+ private String[] getImageCommand(File tempFile) {
+ List<String> commands = new ArrayList<String>();
+ commands.add(imageCommandPart1);
+ commands.add(String.format(midPart, tempFile.getAbsolutePath()));
+ commands.addAll(Arrays.asList(imageCommandPart2));
+ return commands.toArray(new String[commands.size()]);
+ }
+
+ private static final String midPart = "set the_file to \"%s\"";
+ private static final String imageCommandPart1 = "set imagedata to artwork of current track";
+ private static final String[] imageCommandPart2 = {
+ "try",
+ " open for access the_file with write permission",
+ " set eof of the_file to 0",
+ " write (imagedata) to the_file starting at eof",
+ " close access the_file",
+ "on error",
+ " try",
+ " close access the_file",
+ " end try",
+ "end try"};
}
@@ -9,11 +9,11 @@ import nl.pascaldevink.jotify.gui.listeners.PlayerListener
import nl.pascaldevink.jotify.gui.listeners.PlayerListener.Status
import java.awt.event._
import java.io.IOException
-import java.net.URI
import swing._
import java.awt.{Component, Graphics, Cursor, Desktop, Font, Image}
import javax.swing._
import java.util.concurrent.Semaphore
+import java.net.{URL, URI}
class SpotifyUI(spotifySupport: NativeSpotifySupport, mainMenuBar: JMenuBar, spotifyMenuItem: JMenuItem, preferences: MuseControllerPreferences) extends PlayerListener with MuseControllerFrame {
private val executionLock = new Semaphore(1)
@@ -322,7 +322,10 @@ class SpotifyUI(spotifySupport: NativeSpotifySupport, mainMenuBar: JMenuBar, spo
widgets.pauseButton.enabled = true;
widgets.nextButton.enabled = true;
widgets.previousButton.enabled = true;
- widgets.imageLabel.icon = new ImageIcon(spotifySupport.image(track.getCover).getScaledInstance(130, 130, Image.SCALE_SMOOTH));
+ val imageUrl: URL = spotifySupport.imageUrl(track.getCover)
+ print(imageUrl)
+ val unscaledIcon: ImageIcon = new ImageIcon(imageUrl)
+ widgets.imageLabel.icon = new ImageIcon(unscaledIcon.getImage.getScaledInstance(130, 130, Image.SCALE_SMOOTH));
}
override def playerStatusChanged(status: Status) {

0 comments on commit 6538f7e

Please sign in to comment.