doejo usa / that's a karate chop yo.

doejo

MPMusicPlayerController "Hello World!" iPhone app with source code

June 3rd, 2010 in iPhone/iPad, Code

Once I was listening to iPod music player on my iPhone and I decided to look up lyrics for a song that was playing. What do you usually do in that case? Right, go to Google in mobile Safari (that takes 40 seconds, AT&T 3G hi!). What next? Navigate through several adful websites and finally, yes! The good one that has lyrics… Wait, now you’re listening to a completely different song, ughh.

So, the idea came to my mind, why not just to create an app that identifies a song that’s playing right now, no Google, no clicking around, just give me my lyrics right away when I open the app!

So, let’s begin. Open XCode and choose a View-based Application.

Let’s download a LoadingView class by Matt Gallagher to make it all fancy, before we dive into the development. I love using this view, there are several reasons: it’s super fast, because it’s using QuartzCore and it’s highly customizable. This view comes full-screen sized but I think it looks better like this:

Also, now we should add QuartzCode and MediaPlayer frameworks to the project.

We’ll be using UIWebView to download lyrics from lyrics.wikia.com (it’s the best source that I found and URLs are very convenient – http://lyrics.wikia.com/Artist:Song).

First, we need to add UIWebViewDelegate protocol to MainViewController class to handle all user clicks inside the UIWebView browser (otherwise it’ll crash), then add some views and methods.

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIWebViewDelegate> {
    IBOutlet UIWebView *webView;
    LoadingView *loadingView;
    IBOutlet UIView *emptyView;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) LoadingView *loadingView;
@property (nonatomic, retain) UIView *emptyView;
- (IBAction)showInfo;
- (IBAction)refreshLyrics;

Ok, now we should create a handler (shouldStartLoadWithRequest) to monitor user clicks inside the UIWebView:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}

Now, let’s get lyrics immediately the main view loads:

- (void)viewDidLoad {
    [self refreshLyrics];
    [super viewDidLoad];
}

There’s also a refresh button, that’s why I’m using - (IBAction) refreshLyrics.

In the refreshLyrics we need to gather information about currently playing song from iPod using MPMusicPlayerController, thanks to iPhone OS3.0 it’s pretty easy:

MPMediaItem *media = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem];
NSString *artist = [media valueForProperty:MPMediaItemPropertyArtist];
NSString *song = [media valueForProperty:MPMediaItemPropertyTitle];

Now, we need to create an URL, it works like that:

NSString *urlAddress = [[NSString alloc] initWithFormat:@"http://lyrics.wikia.com/%@:%@", artist, song];
NSString* escapedUrl = [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

That’s almost it! One thing left – we need to direct the UIWebView to this URL:

NSURL *url = [NSURL URLWithString:escapedUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

The app also shows activity indicator in the status bar, here’s the one liner:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // or NO…

Well, that’s it. Grab the source code here.

P.S. Don’t try to submit it to Apple, because they won’t approve it - lyrics are considered to be a copyrighted material.
However, I don’t care, with Doejo’s Apple developer account I can use it as long as I want :)

P.S.S. There's no garbage collection, if you want to get this thing working properly you need to dealloc objects that were created. Take a look at what Apple recommends here.

Roman Sr. Software Engineer

Comments (3)

Badass :)

by Timur - 06/03/2010 @ 01:30am

D***! Roman, I'm playing around, coding my first iPhone app, it downloads chords and lyrics from sites like chordie and ultimateguitar and saves them. You're saying that Apple won't let me give this away at the App store?

by bosborne - 01/18/2011 @ 08:39am

You can always give it a try. Anyway, it's a good practice for you!

by Roman - 01/20/2011 @ 12:10pm