Caution: libspotify in its current form has been deprecated: https://developer.spotify.com/news-stories/2015/05/26/last-month-news/
The libspotify C API package allows third party developers to write
applications that utilize the Spotify music streaming service.
Spotify is a really nice music streaming service, and being able to interact with it in an API is awesome. libspotify itself is however written in C, making it unavailable or cumbersome to use for many developers.
This project aims to allow Ruby developers access to all of the libspotify C API, without needing to reach down to C. However, to use this library to its full extent you will need to learn how to use the Ruby FFI API.
The Spotify gem has:
You’ll need:
examples/
directory.Running the examples is as simple as:
ruby example-audio_stream.rb
Available examples are:
Almost all functions require you to have created a session before calling them. Forgetting to do so won’t work at best, and will segfault at worst. You'll also want to log in before doing things as well, or objects will never load.
See Spotify::API#session_create for how to create a session. See Spotify::API#session_login for logging in.
When creating objects in libspotify they are not populated with data instantly, instead libspotify schedules them for download from the Spotify backend. For libspotify to do it's work with downloading content, you need to call Spotify::API#session_process_events regularly.
Users who signed up to Spotify with their Facebook account will have numeric IDs as usernames, so a link to their profile looks like spotify:user:11101648092. Spotify Classic users instead have their usernames as canonical name, so a link to their profile looks like spotify:user:burgestrand.
This matters, for example, when you use the function Spotify::API#session_publishedcontainer_for_user_create.
libspotify allows you to pass callbacks that will be invoked by libspotify when events of interest occur, such as being logged out, or receiving audio data when playing a track.
Callbacks can be very tricky. They must never be garbage collected while they are in use by libspotify, or you may get very weird bugs with your Ruby interpreter randomly crashing. Do use them, but be careful.
The Spotify gem has very few opinions. It is build to closely resemble the libspotify C API, and has very little to aid you in terms of how to structure your application. It aims to make calling the libspotify C API frictionless, but not much more. It is up to you to decide your own path.
Given a version X.Y.Z
, each segment corresponds to:
X
reflects supported libspotify version (12.1.45 => 12). There are no guarantees of backwards-compatibility!Y
is for backwards-incompatible changes.Z
is for backwards-compatible changes.You should use the following version constraint: gem "spotify", "~> 12.5.3"
.
By default, Spotify uses the libspotify gem which means you do not need to install libspotify yourself. However, if your platform is not supported by the libspotify gem you will need to install libspotify yourself.
Please note, that if your platform is not supported by the libspotify gem I’d very much appreciate it if you could create an issue on libspotify gem issue tracker so I can fix the build for your platform.
Instructions on installing libspotify manually are in the wiki: How to install libspotify