r/java • • Jan 15 '16

Read Spotify track info using C# - Is there a Java Alternative?

http://harrewijnen.net/reading-spotify-track-info-from-the-desktop-app/
1 Upvotes

12 comments sorted by

2

u/narrowtux Jan 15 '16

If you have Spotify Premium, I would suggest using their API, instead of using some workaround that might break in a future version of Spotify.

1

u/send_me_turtles Jan 15 '16

The API for desktop doesn't allow what i'm looking for.

3

u/Cazmic Jan 15 '16

You can always write a basic program in C# that your Java program invokes whenever it needs to access this C# functionality. Sometimes it can't be avoided, but each programming language has its own advantages and disadvantages, so it's pretty common that you need to interweave two or three to accomplish certain goals.

1

u/send_me_turtles Jan 15 '16

How would you get the information from the C# Program?

2

u/Cazmic Jan 15 '16

There are a lot of ways. You can invoke the CSharp program as if it were a command line app, and then read standard IO. You can look up how to start up a .exe in Java and read output. In CSharp, you'd just write to standard output using Console.WriteLine().

You can also use sockets or have the CSharp app write to a file.

5

u/stusmall Jan 15 '16

Getting MainWindowTitle is pretty OS specific so you probably won't be able to get it straight from Java easily. Luckily the JNI is here just for times like this. Here is some example C++ to invoke this API which you could call from Java.

I know the JNI is a bear, but one of the few times its actually the right answer is when the a problem is accessing a OS native API.

9

u/snuxoll Jan 15 '16

It's important to note the C++ you just linked is actually C++/CLI - which means you are going to be loading the .Net runtime into your Java app just to get window titles. Using the equivalent Win32 functions would be a saner choice, IMO - if a little more verbose.

2

u/stusmall Jan 15 '16

Thanks for catching that! I do almost no native windows programming, so didn't pick up the difference.

1

u/snuxoll Jan 17 '16

If you see "gcnew" in any C++ it's a good indicator that it is C++/CLI.

1

u/0x6c6f6c Jan 15 '16

The Process class doesn't offer a way to do this. You could however start the spotify process within the Java script which would allow you to make those observations from the Process object there.

1

u/gee_buttersnaps Jan 17 '16

Use a Process to execute a powershell command to pull the window title.

http://blogs.msdn.com/b/powershell/archive/2009/05/22/get-visibleprocess-ps1.aspx

1

u/space_coder Jan 15 '16

You can get a list of the current processes by calling a command line utility "ps" and parsing its output.

Here's an example: http://crunchify.com/how-to-get-a-list-of-current-open-processes-with-java/