Advertisement
Scroll to top

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 PageMusicBrainz Artist PageMusicBrainz 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 PageArtist Release PageArtist 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 PageArtist Release PageArtist 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 PageArtist Release PageArtist 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.

1
2
<!DOCTYPE html> 
3
<html> 
4
	<head>
5
        <meta charset="utf-8">
6
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
7
        <title>MusicBrainz - Magazine</title> 
8
        <link rel="stylesheet" href="jquery.mobile-1.0/jquery.mobile-1.0.css"/>
9
        <script src="jquery-1.7.js"></script>
10
        <script src="jquery.mobile-1.0/jquery.mobile-1.0.js"></script>
11
    </head> 
12
    <body> 
13
        
14
        <div data-role="page" class="type-interior">
15
            
16
            <div data-role="header" data-theme="b">
17
                <h1>Popular Artists</h1>
18
                <a href="#" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
19
            </div><!-- /header -->
20
            
21
            <script type="text/javascript">
22
                var admob_vars = {
23
                    pubid: 'a14e4b353ccfa57', // publisher id

24
                    bgcolor: '356FA8', // background color (hex)

25
                    text: 'FFFFFF', // font-color (hex)

26
                    test: false // test mode, set to false to receive live ads

27
                };
28
                </script>
29
            <script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>
30
            
31
            <div data-role="content">
32
                <div class="content-primary">	
33
                    <ul data-role="listview" data-filter="true">
34
                        <li><a href="650e7db6-b795-4eb5-a702-5ea2fc46c848.html">Lady Gaga</a></li>
35
                        <li><a href="cc197bad-dc9c-440d-a5b5-d52ba2e14234.html">Coldplay</a></li>
36
                        <li><a href="73e5e69d-3554-40d8-8516-00cb38737a1c.html">Rihanna</a></li>
37
                        <li><a href="122d63fc-8671-43e4-9752-34e846d62a9c.html">Katy Perry</a></li>
38
                        <li><a href="f82bcf78-5b69-4622-a5ef-73800768d9ac.html">Jay-Z</a></li>
39
                        <li><a href="d5be5333-4171-427e-8e12-732087c6b78e.html">Black Eyed Peas</a></li>
40
                        <li><a href="20244d07-534f-4eff-b4d4-930878889970.html">Taylor Swift</a></li>
41
                        <li><a href="eeb1195b-f213-4ce1-b28c-8565211f8e43.html">Guns N' Roses</a></li>
42
                        <li><a href="e21857d5-3256-4547-afb3-4b6ded592596.html">Gorillaz</a></li>
43
                        <li><a href="72c536dc-7137-4477-a521-567eeb840fa8.html">Bob Dylan</a></li>
44
                        <li><a href="65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab.html">Metallica</a></li>
45
                        <li><a href="45a663b5-b1cb-4a91-bff6-2bef7bbfdd76.html">Britney Spears</a></li>
46
                        <li><a href="f5dfa020-ad69-41cd-b3d4-fd7af0414e94.html">Wiz Khalifa</a></li>
47
                        <li><a href="ada7a83c-e3e1-40f1-93f9-3e73dbc9298a.html">Arctic Monkeys</a></li>
48
                        <li><a href="e0140a67-e4d1-4f13-8a01-364355bee46e.html">Justin Bieber</a></li>
49
                        <li><a href="3d2b98e5-556f-4451-a3ff-c50ea18d57cb.html">Aerosmith</a></li>
50
                        <li><a href="0103c1cc-4a09-4a5d-a344-56ad99a77193.html">Avril Lavigne</a></li>
51
                        <li><a href="d8354b38-e942-4c89-ba93-29323432abc3.html">30 Seconds to Mars</a></li>
52
                        <li><a href="d262ea27-3ffe-40f7-b922-85c42d625e67.html">PitBull</a></li>
53
                        <li><a href="35f866dc-c061-48ba-8157-cf2e0eac4857.html">Enrique Iglesias</a></li>
54
                    </ul>
55
                </div><!--/content-primary -->		
56
            </div><!-- /content -->
57
            
58
            <div data-role="footer" class="footer-docs" data-theme="b">
59
                <h2>Data from MusicBrainz.org</h2>
60
            </div>
61
	   </div><!-- /page -->
62
    </body>
63
</html>

Running the Xcode project now produces the following result:

MusicBrainz Home PageMusicBrainz Home PageMusicBrainz 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 FooterMusicBrainz Home Page FooterMusicBrainz 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:

1
2
<!DOCTYPE html> 
3
<html> 
4
	<head>
5
        <meta charset="utf-8">
6
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7
            <meta name="viewport" content="width=device-width, initial-scale=1"> 
8
                <title>{ARTIST_NAME}</title> 
9
                <link rel="stylesheet" href="jquery.mobile-1.0/jquery.mobile-1.0.css"/>
10
                <script src="jquery-1.7.js"></script>
11
                <script src="jquery.mobile-1.0/jquery.mobile-1.0.js"></script>
12
    </head> 
13
    <body> 
14
        
15
    <div data-role="page" class="type-interior">
16
            <div data-role="header" data-theme="b">
17
                <a href="index.html" data-icon="back" data-iconpos="back" class="ui-btn-left jqm-home">Back</a>
18
                <h1>{ARTIST_NAME}</h1>
19
                <a href="#" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
20
                
21
            </div><!-- /header -->
22
            
23
            <div data-role="content">
24
                <div class="content-primary">	
25
                <script type="text/javascript">
26
                    var admob_vars = {
27
                        pubid: 'a14e4b353ccfa57', // publisher id

28
                        bgcolor: '356FA8', // background color (hex)

29
                        text: 'FFFFFF', // font-color (hex)

30
                        test: false // test mode, set to false to receive live ads

31
                    };
32
                    </script>
33
                <script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>
34
                
35
                    
36
                <div data-role="collapsible" data-collapsed="false">
37
                    <div id='lifeSpanBegin'>Born: {ARTIST_BIRTH_DATE}</div> 
38
                    <div id='gender'>Gender: {ARTIST_GENDER} </div>
39
                    <div id='country'>Country: {ARTIST_COUNTRY} </div>
40
                    <div id='alias'>Alias:  {ARTIST_ALIAS} </div>
41
                    <div id='tags'>Tags: {ARTIST_TAGS}</div>
42
                </div>
43
                    
44
                    <div data-role="collapsible-set">
45
                        
46
                        <div data-role="collapsible" data-collapsed="false">
47
                            <h3>Releases</h3>
48
                            <ul data-role="listview" data-inset="true">
49
                                 {RELEASES_LIST}
50
                            </ul>
51
                        </div>
52
                        
53
                        <div data-role="collapsible">
54
                            <h3>Links</h3>
55
                           <ul data-role="listview" data-inset="true">
56
                                 {LINKS_LIST}
57
                            </ul>
58
                        </div>
59
                        
60
                    </div>
61
                </div><!--/content-primary -->		
62
            </div><!-- /content -->
63
            
64
            <div data-role="footer" class="footer-docs" data-theme="b">
65
            <h2>Data from MusicBrainz.org</h2>
66
            </div>
67
            </div><!-- /page -->
68
    </body>
69
</html>

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.

1
2
<?php
3
header("Content-type: text/html; charset=utf-8");
4
5
include 'classes/artist.php';
6
include 'classes/release.php';
7
include 'classes/relation.php';
8
9
$artist         = new artist();
10
$release        = new release();
11
$relation       = new relation();
12
$artistTemplate = file_get_contents("artistTemplate.html");
13
$outputFolder   = "Data";
14
15
$artistId	=	array("650e7db6-b795-4eb5-a702-5ea2fc46c848", "cc197bad-dc9c-440d-a5b5-d52ba2e14234", "73e5e69d-3554-40d8-8516-00cb38737a1c",
16
					  "122d63fc-8671-43e4-9752-34e846d62a9c", "f82bcf78-5b69-4622-a5ef-73800768d9ac", "d5be5333-4171-427e-8e12-732087c6b78e",
17
					  "20244d07-534f-4eff-b4d4-930878889970", "eeb1195b-f213-4ce1-b28c-8565211f8e43", "e21857d5-3256-4547-afb3-4b6ded592596",
18
					  "72c536dc-7137-4477-a521-567eeb840fa8", "65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab", "45a663b5-b1cb-4a91-bff6-2bef7bbfdd76",
19
					  "f5dfa020-ad69-41cd-b3d4-fd7af0414e94", "ada7a83c-e3e1-40f1-93f9-3e73dbc9298a", "e0140a67-e4d1-4f13-8a01-364355bee46e",
20
					  "3d2b98e5-556f-4451-a3ff-c50ea18d57cb", "0103c1cc-4a09-4a5d-a344-56ad99a77193", "d8354b38-e942-4c89-ba93-29323432abc3",
21
					  "d262ea27-3ffe-40f7-b922-85c42d625e67", "35f866dc-c061-48ba-8157-cf2e0eac4857");
22
23
foreach ($artistId as $id) 
24
{
25
        $MBArtistQuery 		   = "http://musicbrainz.org/ws/2/artist/?query=arid:$id";
26
        $MBArtistReleasesQuery = "http://musicbrainz.org/ws/2/release/?query=arid:$id";
27
        $MBArtistLinksQuery    = "http://musicbrainz.org/ws/2/artist/$id?inc=url-rels";
28
29
        $fileHandle = fopen("$outputFolder/$id.html", "w+");
30
        $artistXML  = simplexml_load_file("$MBArtistQuery");
31
        $artistData = showArtist($id, $artistXML, $artistTemplate);
32
		
33
        sleep(20);
34
35
        $artistReleaseXML = simplexml_load_file("$MBArtistReleasesQuery");
36
        $artistData	      = showArtistReleases($id, $artistReleaseXML, $artistData);
37
        
38
		sleep(20);
39
40
        $artistLinksXML = simplexml_load_file("$MBArtistLinksQuery");
41
        $artistData     = showArtistLinks($id, $artistLinksXML, $artistData);
42
        fwrite($fileHandle, $artistData);
43
        fclose($fileHandle);
44
        sleep(20);
45
}
46
47
function showArtistLinks($artistId, $artistXML, $artistTemplate) 
48
{
49
    $links = "";
50
    foreach ($artistXML->artist->{'relation-list'}->relation as $artistsRelation)
51
	{
52
        $relation->type   = $artistsRelation['type'];
53
        $relation->target = $artistsRelation->target;
54
55
        $links .= "<li><a href='$relation->target'><h4>$relation->type</h4><p><strong>$relation->target</strong></p></a></li>";
56
    }
57
    $artistTemplate = str_replace("{LINKS_LIST}", $links, $artistTemplate);
58
    return $artistTemplate;
59
}
60
61
function showArtistReleases($artistId, $artistReleaseXML, $artistTemplate) 
62
{
63
    $releases = "";
64
    foreach ($artistReleaseXML->{'release-list'}->release as $releaseInfo) {
65
        $release->id     = $releaseInfo['id'];
66
        $release->title  = $releaseInfo->title;
67
        $release->date   = $releaseInfo->date;
68
        $release->status = $releaseInfo->status;
69
        $release->type   = $releaseInfo->{'release-group'}['type'];
70
        $release->format = $releaseInfo->{'medium-list'}->medium->format;
71
        $release->tracks = $releaseInfo->{'medium-list'}->{'track-count'};
72
        $releases 		.= "<li>$release->title<span class='ui-li-count'>$release->date</span></li>\n";
73
    }
74
75
    $artistTemplate = str_replace("{RELEASES_LIST}", $releases, $artistTemplate);
76
    return $artistTemplate;
77
}
78
79
function showArtist($artistId, $artistXML, $artistTemplate) 
80
{
81
    $artist->id            = $artistId;
82
    $artist->name          = $artistXML->{'artist-list'}->artist->name;
83
    $artist->gender        = $artistXML->{'artist-list'}->artist->gender;
84
    $artist->country       = $artistXML->{'artist-list'}->artist->country;
85
    $artist->lifeSpanBegin = $artistXML->{'artist-list'}->artist->{'life-span'}->begin;
86
87
    foreach ($artistXML->{'artist-list'}->artist->{'alias-list'}->alias as $artistAlias) {
88
        $artist->alias.=$artistAlias . ",";
89
    }
90
91
    foreach ($artistXML->{'artist-list'}->artist->{'tag-list'}->tag as $artistTags) {
92
        $artist->tags.=$artistTags->name . ",";
93
    }
94
95
    $artistTemplate = str_replace("{ARTIST_NAME}", $artist->name, $artistTemplate);
96
    $artistTemplate = str_replace("{ARTIST_BIRTH_DATE}", $artist->lifeSpanBegin, $artistTemplate);
97
    $artistTemplate = str_replace("{ARTIST_GENDER}", $artist->gender, $artistTemplate);
98
    $artistTemplate = str_replace("{ARTIST_COUNTRY}", $artist->country, $artistTemplate);
99
    $artistTemplate = str_replace("{ARTIST_ALIAS}", $artist->alias, $artistTemplate);
100
    $artistTemplate = str_replace("{ARTIST_TAGS}", $artist->tags, $artistTemplate);
101
102
    return $artistTemplate;
103
}
104
?>

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 PageMusicBrainz Artist Details PageMusicBrainz 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 AppMusicBrainz iPhone AppMusicBrainz 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.

1
2
CACHE MANIFEST
3
index.html
4
35f866dc-c061-48ba-8157-cf2e0eac4857.html
5
d262ea27-3ffe-40f7-b922-85c42d625e67.html
6
d8354b38-e942-4c89-ba93-29323432abc3.html
7
0103c1cc-4a09-4a5d-a344-56ad99a77193.html
8
3d2b98e5-556f-4451-a3ff-c50ea18d57cb.html
9
e0140a67-e4d1-4f13-8a01-364355bee46e.html
10
ada7a83c-e3e1-40f1-93f9-3e73dbc9298a.html
11
f5dfa020-ad69-41cd-b3d4-fd7af0414e94.html
12
45a663b5-b1cb-4a91-bff6-2bef7bbfdd76.html
13
65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab.html
14
72c536dc-7137-4477-a521-567eeb840fa8.html
15
e21857d5-3256-4547-afb3-4b6ded592596.html
16
eeb1195b-f213-4ce1-b28c-8565211f8e43.html
17
20244d07-534f-4eff-b4d4-930878889970.html
18
d5be5333-4171-427e-8e12-732087c6b78e.html
19
82bcf78-5b69-4622-a5ef-73800768d9ac.html
20
122d63fc-8671-43e4-9752-34e846d62a9c.html
21
73e5e69d-3554-40d8-8516-00cb38737a1c.html
22
cc197bad-dc9c-440d-a5b5-d52ba2e14234.html
23
650e7db6-b795-4eb5-a702-5ea2fc46c848.html

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:

1
2
[pageControl setDataSource:[[[KGLocalFileDataSource alloc] initWithPath:@"Data"] autorelease]];

to

1
2
[pageControl setDataSource:[[[KGHTMLManifestDataSource alloc] initWithPath:@"Data/custom.manifest"] autorelease]];

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

1
2
[pageControl setScale:1.0];

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
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
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.