Log In

Unity Media Keys API Integration

Integrating with the Unity API is simple, and will enable users to control music playback within your app using the native media keys and/or convenient popup controls provided by the Unity Media Keys Chrome extension.

Include the Unity Shim

Include this script at the bottom of your page, and create the shim object. You can reference it from our servers, or copy it to your own.
<script src="https://s3.amazonaws.com/SwayFM/UnityShim.js" />

var unity = UnityMusicShim();
The shim provides the basis for talking with the Unity extension. It's safe to include for all users -- it gracefully degrades if the Unity extension isn't enabled on the user's system.

Tell us what you support

The first thing you'll do is tell us what features you support so that we can show the correct buttons in the player, and send you the correct events. You'll call the setSupports method and supply an object with truthful values for the keys indicating the features you support. Supported features are playpause, next, previous, thumbsUp, thumbsDown, and favorite.
  playpause: true,
  next: true,
  previous: true,
  favorite: true

Listen for events from the user

The proxy object allows you to create a callback object, and register it with setCallbackObject. You pass this function an object that has functions for each key of an action you support. If you support skipping songs (and you told us via setSupports), include a function in the object under the 'next' key and it will be called when the user presses the 'next' key on their keyboard.
note that Unity won't begin sending events to your site until you indicate music has started playing by sending us an initial playing:true message via sendState.
  pause: function() {
    log("Recieved playpause command");
  next: function() {
    log("Recieved next command")
  previous:function() {
    log("Recieved previous command");
  favorite: function() {
    log("Recieved favorite command")

Update us with your player status

The proxy object provides a sendState method allowing you to provide us with the player state of your app. Whenever your player is paused, the song changes, or the user favorites a song, call sendState with the new state to be reflected in Unity.
var playerState = {
  playing: true,
  title: "Song Title",
  artist: "Artist Name",
  favorite: false,
  albumArt: "http://yoursite.com/path/to/public/image.png",

That's it!

Email us at hello@sway.fm with any issues, or to tell us about any successes!