Nokia Music API for Windows 8

Nokia Music API for Windows 8

Nokia Music API is a set of services that allow you to offer parts of Nokia Music services in your application, which is especially interesting on Windows Phone 8 and (more recently) Windows 8 platform. The Windows 8 support is available for around 2 weeks. How to use it in your app?

Using Nokia Music, your users can find music from

  • over 200 countries
  • over 3 million global and local artists
  • over 20 million tracks
  • over a century of recordings

REST APIs are also available, so you can use Nokia Music on pretty much any platform.

Nokia Music basic how-to

Start a new Windows 8 project or use your existing one. The Nokia Music SDK is available over NuGet, but there is an issue in this version and I couldn't install it this way. When you try installing it, it fails, so the solution is EDIT: The Nuget package has been fixed, you can now add it to your project this way. The whole project can be found on GitHub along with some demos for both Windows 8 and Windows Phone 8 platforms. Open it in Visual Studio and build it after which you'll have a .dll that you can add to references, called Nokia.Music.Win8.

After you add Nokia Music to your project references, the next step is to register your app on Nokia developer portal.

Nokia Music portal

This gives you App ID and AppCode, your app credentials, which is needed by the APIs for each call to the Music services. Launchers don't require application credentials.

Nokia Music app registration

You'll only need AppID for the constructor of the MusicClient object which is the object you need to make the calls. All the methods are asynchronous, so you can use the familiar async/await pattern. You'll be needing the following references:

using Nokia.Music.Types;  
using Nokia.Music.Tasks;  

Let's start by getting all the available genres:

protected async override void OnNavigatedTo(NavigationEventArgs e)  
{
    MusicClient musicClient = new MusicClient("YOUR APP ID");
    var genres = await musicClient.GetGenresAsync();
}

The result is the list of genres like this:

Blues  
Christian  
Classical  
Comedy  
Country  
Dance  
...
...
World  

The 'Result' is not the only thing returned to you. You can also access info such as

  •     Error -- an Exception if the API call failed.
  •     TotalResults -- the total results returned (when appropriate).
  •     ItemsPerPage -- the items per page the API call was asked for (when appropriate).
  •     StartIndex -- the start index the API call was asked for (when appropriate).

Now we can, for example, use one of these genres to get the top artists in that genre

var topArtists = await musicClient.GetTopArtistsForGenreAsync(genres.Result[12]);

which gives us:

Rudimental  
Daft Punk feat. Pharrell Williams  
will.i.am  
Adele  
P!nk  
P!nk featuring Nate Ruess  
Katy Perry  
Justin Timberlake  
....

Next we can take one of those artists and find all of their products

var adeleProducts = await musicClient.GetArtistProductsAsync(topArtists.Result[3]);  

Or we can find artists similar to Adele

var adeleSimilar = await musicClient.GetSimilarArtistsAsync(topArtists.Result[3]);  

which gives us

Coldplay  
Duffy  
Bruno Mars  
Aloe Blacc  
Amy Winehouse  
Alicia Keys  
Maroon 5  
Avril Lavigne  
Cee-Lo Green  
Jessie J  

That's cool, I like Coldplay. I can get the picture from one of their products:

img.Source = new BitmapImage(adeleSimilar.Result[0].Thumb320Uri);  

where img is defined in XAML

<Grid Name="content" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
    <Image Name="img"
           Width="300"
           Height="300" />
</Grid>  

But I won't use them any more for now. I'll rather search for one artist from Croatia.

var someArtist = await musicClient.SearchArtistsAsync("Gibonni");

Which returns this artist as an individual, but also records of him with other artists:

Gibonni  
The Obala & Gibonni  
Stipisic Zlatan - Gibonni  
Dragojevic Oliver I Gibonni  
Gibonni I Vanna  
Jasna Zlokic & Gibonni  
Urban feat. Gibonni  
Stipisic Z. - Gibonni I Josipa Lisac  
Stipisic Z. Gibonni I Mladen Bodalec  
Stipisic Z.- Gibonni,g.bare I M.azucena  

That's great, but I want his individual performances and get the sample track url which can then be played

var giboProducts = await musicClient.GetArtistProductsAsync(someArtist.Result[0]);  
var trackUri = musicClient.GetTrackSampleUri(giboProducts.Result[0].Id);  
MediaElement mysound = new MediaElement();

mysound.AudioCategory = AudioCategory.GameEffects;  
mysound.Source = new Uri(trackUri.AbsoluteUri);  
content.Children.Add(mysound);  
mysound.Play();  

"content" is the name of the Grid defined in XAML.

That's even more cool. All the other methods can be found in the MusicClient metadata with their explanations.

It feels very natural and easy to work with these methods! The result is one of the basic model objects: Product, Artist, MusicItem, Mix, MixGroup, Genre etc. You can find all these types explained in the documentation.

Nokia Music launchers

Launchers make it possible to call the official Nokia Music app on Windows 8 from your own app. Available tasks are

  • LaunchTask - starts the Nokia Music application
  • MusicSearchTask - search for music within Nokia Music app
  • ShowArtistTask - shows details of an artist within Nokia Music app
  • ShowMixesTask - starts Nokia Music and shows the free Mixes
  • PlayMixTask - starts playback of a Mix within Nokia Music

Usage is really simple. To start Nokia Music app, do this:

var tsk = new LaunchTask();  
tsk.Show();

If you wish to search for John, do this:

var tsk = new MusicSearchTask();  
tsk.SearchTerms = "john";  
tsk.Show();

Nokia Music search

Show me more of Coldplay:

var tsk = new ShowArtistTask();  
tsk.ArtistName = "Coldplay";  
tsk.Show();  

Or show me the free Mixes:

var tsk = new ShowMixesTask();  
tsk.Show();  

Nokia Music mixes

And finally, play a Mix:

var tsk = new PlayMixTask();  
tsk.ArtistName = "Coldplay";  
tsk.Show();  

Launchers are sometimes available the other way, too. Remember when we fetched the top artists in Pop genre? We can then use one of those artists and call the PlayMix method which is the same to creating the PlayMixTask and showing it:

topArtists.Result[5].PlayMix();  

Possibilities are great, and so is the Nokia Music service. See if you can use it in your apps. :)

Igor Ralic

igor ralic

View Comments
Microsoft Certified Solutions Developer: Windows Store Apps in C#