Use MusicBrainz in iOS (3) Query complete album information

Source: Internet
Author: User
Tags asin

This article discusses how to obtain complete information about an album by album name, including the song list, artist list, release time, and region.

Because the album information is searched by album name, there may be many results. For example, a "Violin Concertos" may contain multiple search results, this article only shows the complete information of the album and does not perform further matching. Therefore, the first search result is used as an example.

The Code is as follows:

# Import "AlbumViewController. h "# import" MB. h "# import" ResultViewController. h "# define UnknownString @" unknown "# define UnknownInteger 0 @ interface AlbumViewController () @ property (strong, nonatomic) ResultViewController * resultController; @ property (copy, nonatomic) RequestFailureBlock failureBlock; @ end @ implementation AlbumViewController @ synthesize resultController = _ resultController;-(void) viewDidLoad {[ Super viewDidLoad]; self. resultController = [[UIStoryboard storyboardWithName: @ "Main" bundle: nil] instantiateViewControllerWithIdentifier: @ "ResultViewController"]; self. failureBlock = ^ (MBRequest * request, NSError * error, NSData * data) {NSString * message = [NSString stringWithFormat: @ "error: % @", [error localizedDescription]; UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "warning" message: Message delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alertView show] ;}- (void) alertWithMessage :( NSString *) message {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "warning" message: message delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alertView show];}-(IBAction) search :( id) sender {MBConnection * conn = [MBConnection connection]; RequestSucce SsBlock successBlock = ^ (MBRequest * request, MBMetadata * metadata) {comment ist * list = metadata. releaseList; MBRelease * release = [list elementAtIndex: 0]; // album mbid _ resultController. mbid = (release. id )? Release. Id: UnknownString; if (! Release. Id) {[self alertWithMessage: @ "search failed"]; return;} // album name_resultcontroller. title = (release. Title )? Release. Title: UnknownString; // album status _ resultController. Status = (release. status )? Release. Status: UnknownString; // album audio _ resultController. quality = (release. Quality )? Release. Quality: UnknownString; MBTextRepresentation * textRepresentation = release. TextRepresentation; // album language _ resultController. Language = (textRepresentation. language )? TextRepresentation. Language: UnknownString; // album script Language _ resultController. Script = (textRepresentation. script )? TextRepresentation. Script: UnknownString; MBArtistCredit * artistCredit = release. ArtistCredit; NSUInteger nameCount = (artistCredit. elementCount )? ArtistCredit. elementCount: UnknownInteger; // album artist LIST _ resultController. artists = [NSMutableArray array]; for (int I = 0; I <nameCount; I ++) {MBNameCredit * nameCredit = [artistCredit elementAtIndex: I]; [_ resultController. artists addObject: nameCredit. artist. name];} MBReleaseGroup * group = release. releaseGroup; NSLog (@ "Release organization type = % @", group. type); NSLog (@ "Release organization name =%@", group. title); // album release date _ res UltController. date = (release. Date )? Release. Date: UnknownString; // album country _ resultController. Country = (release. country )? Release. country: UnknownString; NSLog (@ "album barcode =%@", release. barcode); NSLog (@ "Asin standard identification code, from excellence Amazon =%@", release. asin); optional ist * mediumList = release. mediumList; MBMedium * medium = [mediumList elementAtIndex: 1]; // album release form _ resultController. format = medium. format; NSLog (@ "Number of music in the album = % d", medium. trackList. count. integerValue); // display the album details in a list [self showAllData: release. id] ;}; MBRequest * req = [MBRequest searchForEntity: MBEntityRelease query: @ "Violin Concertos" // query: @ "don't want to let go" limit: [NSNumber numberWithInteger: 10] offset: [NSNumber numberWithInteger: 0]; [conn enqueueRequest: req onSuccess: successBlock onFailure: self. failureBlock];}-(void) showAllData :( NSString *) mbid {MBConnection * conn = [MBConnection connection]; MBRequest * req = [MBRequest lookupWithEntity: MBEntityRelease mbid: mbid incParameters :( MBIncParameterRecordings | metrics)]; void (^ successBlock) (MBRequest *, MBMetadata *) = ^ (MBRequest * request, MBMetadata * metadata) {MBRelease * release = metadata. release; if (release) {_ resultController. tracks = [NSMutableArray array]; policist * mediumList = release. mediumList; MBMedium * medium = [mediumList elementAtIndex: 0]; policist * trackList = medium. trackList; for (MBTrack * track in trackList) {[_ resultController. tracks addObject: track. recording. title];} [self. navigationController pushViewController: _ resultController animated: YES]; NSLog (@ "Success");} else {[self alertWithMessage: @ "search failed"] ;}; [conn enqueueRequest: req onSuccess: successBlock onFailure: self. failureBlock];} @ end

In fact, this is the same as the search method in Use MusicBrainz in iOS (2), but the search type is MBEntityRelease. After searching for and obtaining the first result in the list, we can select its mbid for further query, because only through the mbid query can we find the information about the songs contained in the album.

After the query is successful, use a table to load the data. The ResultViewController class code is as follows:

#import 
 
  @interface ResultViewController : UITableViewController@property (strong, nonatomic) NSMutableArray *tracks;@property (strong, nonatomic) NSMutableArray *artists;@property (copy, nonatomic) NSString *title;@property (copy, nonatomic) NSString *mbid;@property (copy, nonatomic) NSString *date;@property (copy, nonatomic) NSString *country;@property (copy, nonatomic) NSString *format;@property (copy, nonatomic) NSString *language;@property (copy, nonatomic) NSString *script;@property (copy, nonatomic) NSString *quality;@property (copy, nonatomic) NSString *status;@end
 

# Import "ResultViewController. h "@ interface ResultViewController () @ end @ implementation ResultViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; self. navigationItem. title = self. title ;}# pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 4;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {switch (section) {case 0: return [self. tracks count]; case 1: return [self. artists count]; case 2: return 5; case 3: return 2; default: return 0;}-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {switch (section) {case 0: return @ "music list"; case 1: return @ "artist"; case 2: return @ "basic info"; case 3: return @ "Additional Information"; default: return @ "" ;}}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "CellIdentifier" forIndexPath: indexPath]; if (indexPath. section = 0) {cell. textLabel. text = self. tracks [indexPath. row];} else if (indexPath. section = 1) {cell. textLabel. text = self. artists [indexPath. row];} else if (indexPath. section = 2) {NSString * text; switch (indexPath. row) {case 0: text = [NSString stringWithFormat: @ "Release Date: % @", self. date]; break; case 1: text = [NSString stringWithFormat: @ "region: % @", self. country]; break; case 2: text = [NSString stringWithFormat: @ "Release form: % @", self. format]; break; case 3: text = [NSString stringWithFormat: @ "language: % @", self. language]; break; case 4: text = [NSString stringWithFormat: @ "Script: % @", self. script]; break; default: break;} cell. textLabel. text = text;} else if (indexPath. section = 3) {NSString * text; switch (indexPath. row) {case 0: text = [NSString stringWithFormat: @ "Quality: % @", self. quality]; break; case 1: text = [NSString stringWithFormat: @ "status: % @", self. status]; break; default: break;} cell. textLabel. text = text;} return cell;} @ end


The running result is as follows:



The program may be modified to see if any further requirements are raised tomorrow.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.