Skip to content

Using libjahspotify

nvdweem edited this page Sep 2, 2012 · 4 revisions

This page will show a few examples on how to use libjahspotify in your application.

Initializing & logging in

You are required to initialize libjahspotify before using it. This example will show you how to initialize the library and login after it is initialized.

public class Main {
	public static void main(String[] args) {
		// Determine the tempfolder and make sure it exists.
		File temp = new File(new File(Main.class.getResource("Main.class").getFile()).getParentFile(), "temp");
		temp.mkdirs();
		
		// Start JahSpotify
		JahSpotifyService.initialize(temp);
		JahSpotifyService.getInstance().getJahSpotify().addConnectionListener(new AbstractConnectionListener() {
			@Override
			public void initialized(boolean initialized) {
				
				// Ask for the username and password.
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				String username = null, password = null;
				try {
					System.out.print("Username: ");
					username = in.readLine();
					System.out.print("Password: ");
					password = in.readLine();
				} catch (IOException e) {
					e.printStackTrace();
					System.exit(1);
				}
				
				// When JahSpotify is initialized, we can attempt to login.
				if (initialized)
					JahSpotifyService.getInstance().getJahSpotify().login(username, password, null, false);
			}
			
			@Override
			public void loggedIn(boolean success) {
				if (success)
					System.out.println("Logged in! You can now use all features");
			}
		});
	}

}

Using links

Links are used to specify all kinds of stuff in Spotify. Libjahspotify can also use these links:

Link trackLink = Link.create("spotify:track:6zX1edWck5DxIBWQMOUXO5");
Track track = JahSpotifyService.getInstance().getJahSpotify().readTrack(trackLink, null);

After the track is loaded it can be used to show information about it, or to play it.

Loadable objects

Most of the methods in libjahspotify are asynchronous. This means that a result will be given immediately, but the data within the result might not be complete.

The classes which will be completed after they are returned implement the Loadable interface. This gives them an isLoaded() function to determine if it is safe to show the data within. To wait for a class to be loaded you can use the

MediaHelper.waitFor([loadable], [timeout]);

method.

If is also posible to wait non-blocking by adding a callback to the loadable object:

JahSpotifyImpl.getInstance().readTrack(Link.create("spotify:track:1S8kpJsUzu0wAfaPdVT9JQ")).addLoadableListener(new LoadableListener<Track>() {
	public void loaded(Track media) {
		System.out.println("Track ("+media.getTitle()+") loaded");
	}
});

Media player

Libjahspotify can play the tracks from Spotify. Playing will be done by using the MediaPlayer class. You could use the JahSpotify.play method, but then you will miss a lot of information about the playing track.

The best way to play tracks is by creating a queue and add it to the MediaPlayer. After that you can fill the queue and instruct the MediaPlayer to play a track from the queue.

// Get tracks and let them load.
ArrayList<Track> tracks = new ArrayList<Track>();
tracks.add(JahSpotifyService.getInstance().getJahSpotify().readTrack(Link.create("spotify:track:6zX1edWck5DxIBWQMOUXO5"), null));
tracks.add(JahSpotifyService.getInstance().getJahSpotify().readTrack(Link.create("spotify:track:3gNNXc7GUgKRPR15W77eDR"), null));
tracks.add(JahSpotifyService.getInstance().getJahSpotify().readTrack(Link.create("spotify:track:168EgPtyruoUGhrXzEueWq"), null));
tracks.add(JahSpotifyService.getInstance().getJahSpotify().readTrack(Link.create("spotify:track:2YLIGC6qiLp1cymRmg7tWx"), null));
tracks.add(JahSpotifyService.getInstance().getJahSpotify().readTrack(Link.create("spotify:track:2akY1ilCFDpmpesr7UuCCi"), null));
MediaHelper.waitFor(tracks, 10);

// Add the tracks to a queue
Queue<Link> queue = new LinkedList<Link>();
for (Track t : tracks)
	queue.add(t.getId());

// Add the queue, and play the tracks.
MediaPlayer.getInstance().addQueue(queue);
MediaPlayer.getInstance().play();

Controlling the track (volume, position, seeking, skipping etc) can be done through the MediaPlayer.

Searching

Searching is also quite easy, it can be done like this:

jahspotify.Search search = new jahspotify.Search(Query.token("Call me maybe"));
SearchResult result = SearchEngine.getInstance().search(search);
MediaHelper.waitFor(result, 10);

After this, the result object will be filled with the search results. The specifics of the search query can be changed by setting properties to the search object before calling SearchEngine.search.