Web Playback API

The Rdio Developer site has moved, please find this page at its new home.

The Rdio player API allows web developers to incorporate the playback of licensed Rdio music into their web applications. Rdio subscribers and trial users will be able to hear full-length songs and non-users will be able to hear music previews.

It consists of an Adobe Flash file (SWF) with simple JavaScript and ActionScript 3 APIs. It can only be used in compliance with the Rdio API terms and conditions.

There is a Playback API Console available that allows interactive experimentation with the API.

Playback Model

Rdio uses the concept of a source for playback. A source is a sequence of one or more tracks that can be played. Albums, playlists, stations and individual tracks are all different types of sources. In addition to the currently playing source, Rdio can maintain a queue of sources to be played after the current source completes playing. You can see this in action on the rdio.com site. Each Rdio player SWF will have its own current source and queue.

JavaScript Interface

The API SWF must be embedded from http://www.rdio.com/api/swf/ with the following flashvars arguments:

playbackToken
a valid playback token, retrieved from the getPlaybackToken Web Service API method
domain
the domain that the SWF will be embedded in
listener
the name of a JS object to receive callbacks

The easiest way to embed the SWF is probably to use the SWFObject library.

Status callbacks will be called on whatever object was passed in as the callback parameter. For example if foo was supplied then the ready callback will be called as window.foo.ready().

Methods can be called directly on the embed object — the <embed> or <object> tag's DOM node. Since Flash itself defines some methods all methods are prefixed with rdio_.

To call the play method on an API SWF with ID bar you could call:
document.getElementById('bar').rdio_play()
No methods may be called until the ready callback has been received. All calls are asynchronous, state changes will be reflected through callbacks, not through method return codes.

A complete example is available in Github: https://github.com/rdio/hello-web-playback

jQuery Interface

Rdio provides a jQuery library to simplify embedding and interacting with the Web Playback SWF. It is available on GitHub: https://github.com/rdio/jquery.rdio.js. jquery.rdio.min.js should be included after jQuery itself.

The library will embed the SWF inside a placeholder HTML element. Callbacks are exposed as jQuery events on that placeholder element.

If the placeholder is a <div id="api">, the SWF can be embedded with:
$('#api').rdio(playbackToken)
where playbackToken is a valid playback token from the getPlaybackToken Web Service API method.

Callbacks are exposed as jQuery events in the rdio namespace, so the ready event could be captured as:
('#api').bind('ready.rdio', function(event, userInfo) { ... });
Callback arguments become event handler arguments, after the event argument.

Call methods like: $('#api').rdio().play('a997982');

A complete example is available in Github: https://github.com/rdio/jquery.rdio.js/blob/master/example/example.html

ActionScript 3 Interface

The API SWF must be loaded from http://www.rdio.com/api/swf/ and with two query string arguments:

playbackToken
a valid playback token, retrieved from the getPlaybackToken Web Service API method
domain
the domain that the SWF will be embedded in

For example, with a domain of www.example.com and a playbackToken of WXYZ you would load:
http://www.rdio.com/api/swf/?domain=www.example.com&playbackToken=WXYZ

Callbacks are implemented as AS3 events. Once the movie has loaded (Event.INIT) event handlers can be attached to the Rdio object. No calls should be made until the rdioReady event is received. Callback arguments are available as properties on the event object.

This is a brief example:

Security.allowDomain("www.rdio.com");
var rdio : Object = null;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, function() {
  rdio = loader.content;
  rdio.addEventListener("rdioReady", 
    function(event : Event) : void { rdio.play("a997982"); });
  rdio.addEventListener("playingTrackChanged", playingTrackChanged);
});
loader.load(new URLRequest("http://www.rdio.com/api/swf/?"+
  'domain='+encodeURIComponent(domain)+'&'+
  'playbackToken='+encodeURIComponent(playbackToken)));

A complete example is also available in Github: https://github.com/rdio/hello-flash-playback

Methods

The Rdio object exposes a set of methods to fully control playback. From JavaScript the methods are called on the embed object. From ActionScript 3 they are called on the loaded movie object.

play

JS:rdio_play([key, [options]])
jQuery:play([key, [options]])
AS3:play([key, [options]])

Begins playback. It can take two optional arguments, a key string that indicates a new source to play and an options object. When they're not passed in it either resumes play or plays the next item in the queue. If they are passed in, the current playing source is replaced with the specified one. Currently the only supported option is initialPosition, a number that indicating the number of seconds into the track to begin playing from.

pause

JS:rdio_pause()
jQuery:pause()
AS3:pause()

Pauses playback. It can be resumed by calling play with no source specified.

stop

JS:rdio_stop()
jQuery:stop()
AS3:stop()

Stops playback.

next

JS:rdio_next([superSkip])
jQuery:next([superSkip])
AS3:next([superSkip])

Skips to the next track. It takes an optional superSkip boolean parameter. If you pass true then it will skip to the start of the next source in the queue.

previous

JS:rdio_previous()
jQuery:previous()
AS3:previous()

Skips to the beginning of the current track, or if already at the beginning, to the beginning of the previous track.

seek

JS:rdio_seek(position)
jQuery:seek(position)
AS3:seek(position)

Seeks within the currently playing track. It takes a position number argument for the number of seconds into the track to seek.

setShuffle

JS:rdio_setShuffle(shuffle)
jQuery:setShuffle(shuffle)
AS3:setShuffle(shuffle)

Enables or disables shuffle, based on the boolean shuffle argument.

setRepeat

JS:rdio_setRepeat(mode)
jQuery:setRepeat(mode)
AS3:setRepeat(mode)

Sets the repeat mode based on an integer argument mode that can either be: 0no-repeat, 1track-repeat or 2whole-source-repeat.

queue

JS:rdio_queue(key, [options])
jQuery:queue(key, [options])
AS3:queue(key, [options])

Adds a source to the end of the queue.

setVolume

JS:rdio_setVolume(volume)
jQuery:setVolume(volume)
AS3:setVolume(volume)

Sets the playback volume, expressed as a number in the range 0 to 1.

setMute

JS:rdio_setMute(mute)
jQuery:setMute(mute)
AS3:setMute(mute)

Mutes or unmutes playback base on a boolean argument.

playQueuedTrack

JS:rdio_playQueuedTrack(position, offset)
jQuery:playQueuedTrack(position, offset)
AS3:playQueuedTrack(position, offset)

Moves the source at position into playback and plays track offset within that source.

moveQueuedSource

JS:rdio_moveQueuedSource(from, to)
jQuery:moveQueuedSource(from, to)
AS3:moveQueuedSource(from, to)

Reorders the queue so that the source at position from is now at to.

clearQueue

JS:rdio_clearQueue()
jQuery:clearQueue()
AS3:clearQueue()

Clears the queue.

setCurrentPosition

JS:rdio_setCurrentPosition(sourceIndex)
jQuery:setCurrentPosition(sourceIndex)
AS3:setCurrentPosition(sourceIndex)

Sets the position within the currently playing source to sourcePosition. For example, if there's an album playing currently then setCurrentPosition(5) will move playback to the 6th track on the album.

removeFromQueue

JS:rdio_removeFromQueue(sourceIndex)
jQuery:removeFromQueue(sourceIndex)
AS3:removeFromQueue(sourceIndex)

Remove the source at sourceIndex from the queue.

sendState

JS:rdio_sendState()
jQuery:sendState()
AS3:sendState()

Pass all of the player state out to the page via the callback interface.

startFrequencyAnalyzer

JS:rdio_startFrequencyAnalyzer(options)
jQuery:startFrequencyAnalyzer(options)
AS3:startFrequencyAnalyzer(options)

Begin frequency analysis. Options is an object that can include:

period: how often to report data, integer milliseconds (default is 50)
frequencies:
the frequency bands to return, 4-band, 4-band-visual, 8-band, 10-band, 26-band, 31-band, or raw.

stopFrequencyAnalyzer

JS:rdio_stopFrequencyAnalyzer()
jQuery:stopFrequencyAnalyzer()
AS3:stopFrequencyAnalyzer()

Stop frequency analysis.

Callbacks

ready(userInfo)

Called once the API SWF has loaded and is ready to accept method calls. The userInfo is an object with four boolean members indicating the current Rdio user's subscription state: isSubscriber (an Rdio subscriber), isTrial (an Rdio trial account), isFree (an Rdio free account), isUnlimited (an unlimited account). There's also a freeRemaining number member that indicates the percentage of a free user's quota is remaining.

playStateChanged(playState)

The playback state has changed. The state can be: 0 → paused, 1 → playing, 2 → stopped, 3 → buffering or 4 → paused.

playingTrackChanged(playingTrack, sourcePosition)

The currently playing track has changed. Track metadata is provided as playingTrack and the position within the playing source as sourcePosition.

playingSourceChanged(playingSource)

The currently playing source changed. The source metadata, including a track listing is inside playingSource.

volumeChanged(volume)

The volume changed to volume, a number between 0 and 1.

muteChanged(mute)

Mute was changed. mute will either be true (for muting enabled) or false (for muting disabled).

positionChanged(position)

The position within the track changed to position seconds. This happens both in response to a seek and during playback.

queueChanged(newQueue)

The queue has changed to newQueue.

shuffleChanged(shuffle)

The shuffle mode has changed. shuffle is a boolean, true for shuffle, false for normal playback order.

repeatChanged(repeat)

The repeat mode change. repeat will be one of: 0 → no-repeat, 1 → track-repeat or 2 → whole-source-repeat.

updateFrequencyData(frequencyData)

Receive a frequency update. The data comes as a string that is a comma separated array of floats in the range 0-1.

playingSomewhereElse()

An Rdio user can only play from one location at a time. If playback begins somewhere else then playback will stop and this callback will be called.

freeRemainingChanged(freeRemaining)

The percentage of free tracks remaining changed. This number will descend towards zero as the user plays more. When freeRemaining reaches 1 then there is just one last song to play.