LibSpotify for Java

Discuss issues related to PS3 Media Server development (only for programmers)

LibSpotify for Java

Postby jim_k » Tue Jan 11, 2011 12:07 am

Hey,

I found PMS over christmas and have already had many happy hours of video streaming.

I'm keen to see a Spotify plugin for PMS so that I can browse my Spotify playlists from my PS3 :)

So, I've done the work to make a java wrapper for LibSpotify that should help make that a reality.

The work is now uploaded to these open-source projects:

libspotify++ - a C++ wrapper for LibSpotify
http://code.google.com/p/libspotify-plusplus/

jlibspotify - a java wrapper for LibSpotify (via LibSpotify++)
http://code.google.com/p/jlibspotify/


Would any PMS experts here be interested in making a PMS plugin around these classes?


I'll offer up these little tit-bits of information to anyone who is interested:
* You'll need to download the LibSpotify library from Spotify.com (I tested with version 0.0.6 http://developer.spotify.com/en/libspotify/overview/)
* You'll need to get your own Spotify username/password, and your own AppKey from Spotify.com - I've only tested this with my spotify premium account
* You'll need to compile up libspotify++ as a static lib - I have only tested in locally on Windows 7
* You'll need to compile up jlibspotify's C++ source as a dynamic library (linking libspotify++ statically) and its' java code as a java package - I have only tested it locally on Windows 7
* There is a small java test app in the jspotify source ( Spotify.Test ) - just add your own username/password, appkey and cache/settings directories and it will iterate through your playlists, dump them to your TTY, and then try to play the first track in your first playlist.
* To use jlibspotify start by making your own subclass of Spotify.Session, and then override the callback functions that you're interested in (i.e. OnGetAudioBufferStats() and OnMusicDelivery() for audio playback )

Last, but not least..
* Spotify.Session loads the libspotify DLL in its' static initializer. It seems to be perfectly fine in my simple java test apps, but has failed with spectacular Java Runtime crashes whenever I try to load it as part of a plugin for PMS. Perhaps I'm doing something stupid, or perhaps there's a DLL clash with PMS?
* The same DLL loading code also fails when trying to 'debug' my test apps from Eclipse, it is perfectly fine if you 'run' them from eclipse or from the command line.

Cheers
Jim.
Last edited by jim_k on Sun Jan 30, 2011 11:40 pm, edited 1 time in total.
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Re: LibSpotify for Java

Postby SharkHunter » Tue Jan 11, 2011 9:04 am

Hi,

I've been looking for this for awhile, but never found the time to do it myself. To do the PMS wrapper should be fairly quickly done (I'll already started with a poor mans version and I'll just need to modify the grooveshark plugin and it should be all done).

I'll have a look and I'll synch the grooveshark github (that would be this nights project) so you (or anyone else) could have a go at it yourself.
SharkHunter
 
Posts: 941
Joined: Tue Jun 01, 2010 8:39 pm

Re: LibSpotify for Java

Postby jim_k » Tue Jan 11, 2011 11:42 pm

Great!

Please feel free to give me a shout if you need anything.
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Re: LibSpotify for Java

Postby scape » Wed Jan 19, 2011 5:35 pm

jim_k - thanks for doing this!

I'm running into some trouble getting the Java example to run in Eclipse - I'm getting UnsatisfiedLinkError, and "LibSpotifyPlusPlus.lib: %1 is not a valid Win32 application". I've built your C++ wrapper .lib, then used that to build the JLibSpotify .dll, then I'm loading both the .lib and the .dll in Session.java with System.loadLibrary...

They both built fine in Visual C++ 2010 Express, so I'm at a loss as to why the static library doesn't do the business in Eclipse. I've had no errors concerning loading JLibSpotify.dll, but that may be because it hasn't been been accessed yet.

As may be obvious, I'm not much of a programmer (and especially not much of a C/C++ programmer), but if you have any suggestions, or can think of any 'gotchas' on the C/C++ compiling/linking side, I'd be grateful to hear them.

Just for information, my JLibSpotify.dll is about 25kb in size and the LibSpotifyPlusPlus.lib is 619kb - do those sound approximately right, for debug builds? And finally, is it the fact that I'm doing Debug builds part of the problem?

Many thanks in advance!

/brendan
scape
 
Posts: 2
Joined: Wed Jan 19, 2011 5:20 pm

Re: LibSpotify for Java

Postby scape » Wed Jan 19, 2011 5:40 pm

Update - sorry, jim_k - I'm a total moron. I'm not supposed to be loading the .lib in the Java project at all... I'm supposed to be loading the 0.0.6 libspotify.dll.

But...the error I get with that is "UnsatisfiedLinkError: Spotify.Session.NativeCreate()I" - any ideas?

Cheers,

/b
scape
 
Posts: 2
Joined: Wed Jan 19, 2011 5:20 pm

Re: LibSpotify for Java

Postby jim_k » Sat Jan 22, 2011 6:50 pm

Hey Brendan,

Glad to hear you're interested in working with LibSpotify too :)

Yep, your java Spotify app should be using Spotify's LibSpotify.dll 0.0.6, and my JLibSpotify.dll .

Are you using the test app that I provided with JLibSpotify? It runs fine here from the command line, and when I 'run' it in eclipse (it crashes with DLL craziness when I try to 'debug' it in Eclipse).

Are you also sure that the DLLs are accessible to your java app, you may need to update your 'path' environment variable to include their location.

If you're still having problems, then let me know the full error message and we'll see what we can do.

Cheers,
Jim
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Re: LibSpotify for Java

Postby jim_k » Sat Jan 22, 2011 7:12 pm

Hey Again,

When you want to get JLibSpotify working with PMS, you may get an error like this:

Exception in thread "main" java.lang.UnsatisfiedLinkError: <YOUR DLL DIRECTORY>/libspotify.dll: Invalid access to memory location
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
.....

I've tracked the cause of this back to two things:

1. The command line in PMS.bat that's used to start up the application.

If I remove the "-Xmx768M" part of the command line, then it starts just fine.

So, my command line now reads:

java -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -classpath update.jar;pms.jar;plugins/*;plugins net.pms.PMS


2. Where/When you load the LibSpotify DLL

It doesn't seem to be happy loading the DLLs from within a plugin JAR, but it will work from a static initializer in net.pms.PMS.

Code: Select all
public class PMS {
   
   static
   {
      System.loadLibrary( "libspotify" );
      System.loadLibrary( "JLibSpotify" );
   }

   ....


I'm putting a small update to the java files up on the JLibSpotify google code page now.

There should be enough there now to write a PMS plugin with JLibSpotify :)

Cheers
Jim
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Re: LibSpotify for Java

Postby jim_k » Sun Jan 30, 2011 11:39 pm

Hey,

Here's a first attempt at a plugin for PMS to support browsing and playback of Spotify Playlists based on LibSpotify.

http://code.google.com/p/pms-plugin-spotify/

It has:
  • playlist browsing
  • album/artist information
  • track playback

The main issue is that you'll need to get your own username/password & appkey from Spotify - check the readme.txt files for details.

It's robust, I have just had it happily playing my playlists for the last couple of hours.

Under the hood, it is serving audio data as raw 16bit, 44100hz, stereo LPCM to the PS3.

There are a few seconds of latency when starting a track, I'm happy to hear from any PMS experts on whether this could be improved.

Cheers
Jim
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Re: LibSpotify for Java

Postby andash » Tue Feb 01, 2011 9:12 pm

Thank you so much, this looks very good. Been waiting for someone to pick up the torch for this as I have very little knowledge about programming.
Is there any way you could compile the plugin and explain in a bit more detail on how to add my username/password & appkey so I can test it?

Again thanks a lot.
andash
 
Posts: 17
Joined: Sat Feb 07, 2009 2:13 am

Re: LibSpotify for Java

Postby jim_k » Fri Feb 04, 2011 12:56 am

Hi,

I've just uploaded a Win32 build of JLibSpotify.dll (the JNI interface between Java & C++ for LibSpotify).
http://code.google.com/p/jlibspotify/do ... potify.dll

The java side of things currently requires you to compile java code from the jspotify and pms-plugin-spotify projects locally with your own 'AppData' class into a jar file :

package Spotify;

public class AppData {

static char g_appKey[] = {
/* YOUR APPKEY - AS LISTED IN YOUR APPKEY.H FILE FROM SPOTIFY */
/* 0x01, 0x02, 0x03, 0x04 etc */

};

// NOTE
static String g_userName = "your_username";
static String g_password = "your_password";

static String g_cacheLocation = "somewhere on your hard drive";
static String g_settingsLocation = "somewhere on your hard drive";
static String g_userAgent = "PMS Spotify Plugin";
}


The java side of things also requires minor edits to the PMS codebase (as described above in the forum topic).

I'm planning to take another look at the weekend and see if I can add a config pane for the Spotify plugin in PMS, so that we won't need this 'AppData' class anymore. I'll also see if I can get rid of the modifications that are currently required to PMS, if that's possible then I should be able to ship a prebuilt JAR plugin file.

Jim.
jim_k
 
Posts: 11
Joined: Sun Jan 02, 2011 2:15 pm

Next

Return to Developers

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests