Question

I have been trying to research being able to play the itunes previews for songs in a game of mine. As far as I can see you need to go through Apple and their Affiliate program. I have found lots of questions and help about doing it in a webpage but not a ios game. Has anyone gone through this or have been able to get the 30 sec previews playing in their game? If so is there a direction you can point me in or a resource that you found helpful?

Was it helpful?

Solution

I can't speak to the legality issues in doing this, or what programs you may/may not need to join, but I can give you a simple example of how to interact with the iTunes Search API. It's all actually pretty straight forward actually, pass the server a specifically formatter URL and get JSON data back regarding the query results.

NSUInteger numberOfResults = 200;
NSString *searchString = @"AC/DC";

NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *finalSearchString = [NSString stringWithFormat:@"https://itunes.apple.com/search?term=%@&entity=song&limit=%u",encodedSearchString,numberOfResults];

NSURL *searchURL = [NSURL URLWithString:finalSearchString];

dispatch_queue_t iTunesQueryQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,  kNilOptions);

dispatch_async(iTunesQueryQueue, ^{

    NSError *error = nil;
    NSData *data = [[NSData alloc] initWithContentsOfURL:searchURL options:NSDataReadingUncached error:&error];

    if (data && !error) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSArray *array = [JSON objectForKey:@"results"];
        NSLog(@"%@",array);

    }
});

Once you receive the JSON data, you'll notice that there is a key for every result called "previewUrl" this data can be passed to a player of your choice (I used MPMoviePlayerController) and from there all you have to do is call play!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top