Advertisement
  1. Code
  2. Mobile Web Apps
Code

Build a MusicBrainz App

by
Difficulty:IntermediateLength:MediumLanguages:

This tutorial will utilize jQuery Mobile, HTML 5, the PugPig framework, and the iOS SDK to create an application using the MusicBrainz API.

With the introduction of the iPad and various Android tablets, magazine styled applications have become extremely popular. Several of the most prominent of these include FlipBoard for iOS, Zinio for Android, and Pulse for both iOS and Android. There is likewise great demand for tools that can provide magazine layout functionality. Some of the popular tools include FlipBoard (via RSS / Twitter imports), OnSwipe, and PugPig. PugPig is an open-source HTML5 publishing platform that allows us to create beautiful newspapers, magazines, and books for the iPad, iPhone, and Android. For this tutorial we will use the PugPig Framework plugin for Xcode 4.2 to create an HTML5 Magazine.

The design of the magazine will be based on jQuery Mobile, although another equally good option is to use Magazine Grid. We go with jQuery Mobile in this tutorial since it offers extensive widgets and excellent page transitioning workflow for mobile web apps.

The content for the Magazine will be powered by MusicBrainz (the Open Music Encyclopedia). We will build offline HTML pages for popular artists with the jQuery UI. We did a random pick of 20 popular artists from SongKick.com.


Introducing the MusicBrainz API

The MusicBrainz website provides a rich collection of music data, ranging from artist information to their latest releases, with complete details of all the song tracks on each album. It aims to be an open music encyclopedia and it must be doing that job well considering that music services like Last.fm, GrooveShark, Pandora, and Echonest use the data from MusicBrainz. MusicBrainz sources this data back to the community via:

We will use the following Web Services to extract the rich artist information from MusicBrainz:

Artist Information

The artist information available within MusicBrainz includes name, birth and death dates, country of origin, gender, aliases, and type of artist (whether single or group). For the sake of our application use case, let's take Lady Gaga as an example artist. The MusicBrainz page for Lady Gaga is http://musicbrainz.org/artist/650e7db6-b795-4eb5-a702-5ea2fc46c848 where 650e7db6-b795-4eb5-a702-5ea2fc46c848 is the unique Artist ID (which is also referenced by third party music sites, like Last.fm and others).

As seen from the screenshot below, our interest is in most of the right column bar (artist info, tags, external links) and the second tab "Releases" described in the next section.

MusicBrainz Artist Page

Artist Release

The artist release contains a list of all albums released by an artist, whether single hits or group performances. It also includes the date of release, label, and even barcode. Just like before, the release ID is unique as well. This release ID can be used to fetch further information from the album, including the Track Lists (songs).

Artist Release Page

Artist Relation

The artist relation defines other artists to another artist.

Similarly there are API calls for release and tracks relations to similar release and tracks.

We are interested in the Artist Relation attribute and specifically the relation with further attribute as "Url", which lists the Artist Urls on different social and music websites like WikiPedia, IMDB, Twitter, MySpace, Youtube and others.

Having glanced through the MusicBrainz Web Services API, let's step further to create our Magazine App.


Step 1: Creating a PugPig App with Xcode

After you have download the Pugpig Xcode template from the PugPig Site, double-click the package to install the template in Xcode. Open Xcode and start a new project by selecting the PugPig template, as shown below:

Artist Release Page

Name it "MusicBrainz - Magazine" and complete the process. In your project directory, create a folder named "Data" and add it your project, this is the folder where PugPig looks for the HTML files to render. Your project should now look like:

Artist Release Page

Now, let's add an index.html to this data folder so we have a strarting point for our magazine app. We use jQuery Mobile's default design here and show a searchable list of Popular Artists. In the index.html file, add the following code, along with the jQuery Mobile framework and jQuery javascript library.

Running the Xcode project now produces the following result:

MusicBrainz Home Page

Double-tap on the app. Somewhere near the footer of the page, it should show you a small window allowing you to browse the other pages (the .html files in the Data folder) like a magazine!

MusicBrainz Home Page Footer

Step 2: Generating HTML Pages with PHP and MusicBrainz Web Services

So far, we have seen how to build the index.html page for our Magazine App, now it is time to look at how we build the artist details page and save them as .html files in the data folder.

We will use PHP and its XML parsing library SimpleXml to parse the MusicBrainz Web Services calls. Before that we will build a simple artist details page Template using jQuery Mobile page layout and CSS. We also put placeholders in the template that will later on be replaced with actual results retrieved from the MusicBrainz Web Services, so we use this page template with real artist data and generate the final artist details page and save it on disk. Here is the artist template:

We now create the PHP file that will access the MusicBrainz Web Services for the 20 artists listed before and generate HTML files for each of them. We have used a PHP Array to store the Artist ID for the 20 Popular Artists. You can also store the same in a database and use PHP-Mysql code to retrieve Artist ID and other information necessary.

MusicBrainz provides its database for developers to download and process on their end, instead of using the Web Services. You can download the database and learn how to configure the same on your local machine.

The artist, release, and relation classes included on the top of index.php are simple class objects to hold each type of information for an artist. Find the same in the PHP folder in the Project Download link.


Step 3: Copying the Generated Artist Files in Your Xcode Project

Run the index.php from your CLI, it should generate 20 HTML files with the format ARTIST-ID.html within the Data folder. Copy these 20 files over to your data folder in your Xcode project from Step 1. Now, run your Xcode project and click on an artist to navigate to its details page. You should see the following result for the iPad:

MusicBrainz Artist Details Page

Do not forget to make your Xcode project as "Universal", so your magazine app works fine on iPhone as well. In our case it does and looks great!

MusicBrainz iPhone App

Step 4: Controlling the Page Order of Your Magazine

PugPig by default show all the HTML files in the data folder irrespective of index.html or other file names without any order. To always show the index.html page first and then the rest of the pages we need to specify a manifest file in the data folder. Name it custom.manifest and add the following content to it.

We also need to tweak a bit of Objective-C code in our project. Open the MusicBrainz_MagazineViewController.m file and replace the following line in the viewDidLoad method:

to

Also, change the scale of the pageControl to 1.0 from 0.4166667 in the same file.

The scale is automatically handle by jQuery Mobile based on the iOS device, so we do not need to scale it down for iPhone.


Enhancements

I have taken the popular artists on tour now and created the HTML pages for them, further enhancements to the app can be done, by automating the PHP process to generate the HTML pages for all the artists and store them in the data folder, thereby creating a complete offline artist magazine. For images, you can take the WikiPedia CC images and include them in the app to make it more appealing.

To make the app more lively, add some Ajax magic and use the rich links, API, RSS, from various music and event websites like eventful.com or songkick.com to create a dynamic page. For example, fetch the latest events or concerts for an artist from eventful.com -food for thought!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.