Using the Xbox Music API in Universal Windows Apps

The Xbox Music API can be used by third party developers (i.e. you and me) to search for artists, songs, albums, etc and also get access to actually play songs.

There are two flavours at present: “unauthenticated” and “authenticated”.

Authenticated access allow playing of full songs and working with user playlists. Playlist changes will automatically roam to user’s other devices. The full access to play full songs requires that the user has an Xbox Music Pass.

Unauthenticated access doesn’t allow access to user playlists, and streaming of music is restrict to 30 second previews.

At present anyone can get unauthenticated access via the Xbox Music RESTful API on Azure Marketplace. Authenticated access is currently limited, you need to apply for the pilot program. I’ve applied for this, hopefully I’ll be accepted so I can understand this part better.

Getting Started with Unauthenticated Access

We need an access key to allow our apps to able to use the (unauthenticated) API. To do this follow these instructions to register and create an application in Azure Marketplace. Don’t worry about the code samples at the bottom of the post, there’s a client API we can use instead.

So now you should have an application registered (you might have to enter some web address in the redirect URI – I’m not sure what this is for at this point).

So here’s a (redacted) screenshot of a registered application:

image

 

It’s the Client ID and the Client secret that we’ll need in just a moment to actually call the API.

There’s an client wrapper available on GitHub. I couldn’t find the NuGet packages on NuGet.org but download the  (or clone from GiHub to your local machine).

Open the client wrapper solution and do a rebuild to check it’s all ok, then run the package.bat to build NuGet packages.

Next create a new app (e.g. a Universal windows app) and add these local NuGet packages. You’ll need to add a new package source in NuGet Package Manager options as shown in the following screenshot.

image

Now install the Microsoft.Xbox.Music.Platform.Client package from your local machine.

An Example of Using the API – Searching for an Artist

First we need to create an instance of an Xbox music client:

client = XboxMusicClientFactory.CreateXboxMusicClient("DontCodeTiredDemo", "A5G3...REDACTED]");

The first parameter is the Application ID we registered above, the second is the Client secret.

Now we have a client instance, we can perform queries against it, for example the following code searches for albums by the Foo Fighters and returns a max of 5 albums from your current country (“geography”). It then loops through them and adds them to an ObservableCollection<Album> in the viewmodel.

private async void ExecuteSearch()
{
    ContentResponse searchResponse = await client.SearchAsync(Namespace.music, "Foo Fighters", filter: SearchFilter.Albums, maxItems: 5, country: null);           

    Albums.Clear();

    foreach (Album albumResult in searchResponse.Albums.Items)
    {
        Albums.Add(albumResult);
    }
}

It only took me an hour or two to figure all this out and create a universal windows app that allows searching of artists, album selection and album art, track selection and then playing (the 30 second unauthenticated) sample in both a Windows 8.1 Store app and a Windows Phone 8.1 app as the following screenshot shows. I’m really excited to find out more about the authenticated access as I already have some cool app ideas to use with Xbox Music integration :)

Untitled

Comments (2) -

  • Heshan

    7/23/2014 3:55:45 AM | Reply

    The current build is not compatible with WP8.1 RT. Its giving an error when try to install  nuget packages.
    "Microsoft.Xbox.Music.Platform.Client could not be installed because it is not comaptible....
    The package targets the fllowing frameworks: .Net Portable,Version=v0.0, Profile=net45+sl50+win+wp80"

    And packages.bat also fails to create packages.
    I don't know how you got this working? Any help would be appreciated.
    Thx!

  • Heshan

    8/10/2014 4:09:38 PM | Reply

    Hi Jason, Thanks for the previous reply. Now only I saw it in my in-box as I was checking here for a reply. I got it working. Do you know how to launch XboxMusic app using a deep link from an artist/album?

Add comment

Loading