3

I am in the planning stages of developing an Android application to control my Spotify player. Since I am running Debian on my system, the options that are available on the Play Store are quite limited (I can't find any functioning apps). Therefore, the programmer inside me tells me to build one. I haven't built an Android application before, but I have good experience with working with APIs so I think this could be a pretty decent starter project for me.

However, I am having some difficulties grasping some of the concepts on how I could implement this. With some research of the Spotify API documentation, I believe the Spotify Apps API is the one I should be using to control playback on the desktop.

On the Android side of things, I would imagine I would use the Spotify Apps API in conjunction with the Libspotify API to receive metadata and display what the application is getting from the desktop playback.

What I am having troubles understanding is how does the Android application talk with the Spotify desktop client? I have a few ideas, but I am unsure...

My thought process so far is as follows:

  1. have a local webserver on the desktop open up and begin listening
  2. on the Android application, connect to the desktop's webserver (how do you do this?)
  3. once connected, the desktop sends metadata/other information to populate the Android application
  4. from the Android UI, hitting a UI button will send a request to the webserver, where it parses the data, completes an action, and then returns the response to the Android application

I am a little lost on what tools I need to learn and use to get this working. Any suggestions to the implementation are greatly appreciated!

Thank you!

user3745117
  • 127
  • 2
  • 11

2 Answers2

3

Your approach is correct.

On host (desktop), open a socket and listen on port 80 (or another port if you wish) and listen. It doesn't need to be a full web server.

On client (Android), to start with send an http request to the desktop' ip address. Desktop responds with a simple web page.

That's how you get the two communicating.

Then you can start POSTing data to the desktop and returning data in the web page.

Then you can move to communicating using JSON rather than web pages.

You can program both the host and client using Qt. You can develop both on the desktop and move the client to Android (or iPad) when you have got it working.

user3717478
  • 863
  • 1
  • 8
  • 15
  • Hi, thanks for the response! This is very helpful, however I have a few questions: 1) What exactly is Qt? I see that it's used to build GUIs comparatively to Java Swing, but what is its application here? You can write code with it, but why use Qt over, say, an Android Eclipse environment? Can I achieve the same thing with Eclipse? 2) I am only proficient in Java and PHP, is this achievable only with these tools? Qt is primarily C++ from what I can gather.Thanks! – user3745117 Jun 19 '14 at 16:20
  • Sorry, you'd really have to know C++ to use Qt. – user3717478 Jun 27 '14 at 20:17
1

Yes, you are on the right track. If you know java then you can use Java Sockets to connect android and desktop. It should work on Debian. On the server side, you need to create a socket on specified port.

ServerSocket serverSocket = new ServerSocket(PORT);

Then wait for a client to connect

Socket clientSocket = serverSocket.accept();

On the client side, create a socket and connect to the server using IP and port

InetAddress serverAddr = InetAddress.getByName(“SERVER_IP”);
Socket socket = new Socket(serverAddr, PORT);

You can control a lot of things on your desktop using java.awt.Robot class. It let's you simulate keyboard and mouse events. So if you have some hotkeys enabled for Spotify then you don't need Spotify APIs to control it. Just simulate keyboard event. So if spacebar is a hotkey for play/pause then run this on server

robot.keyPress(KeyEvent.VK_SPACE);

This article explains this complete process by creating a simple remote control for vlc

Sourabh86
  • 744
  • 10
  • 18