Use MusicBrainz in IOS (iii) query the full information of the album

Source: Internet
Author: User
Tags asin uikit

This article discusses the complete information about getting an album by album name. Contains a list of songs. Artist list, release time and region etc.

Because album information is searched by album name. So the search results may be more, such as a "Violin concertos" may include multiple search results, and this article is only to display the complete information of the album, no further matching work, so take the first search results 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 StringWithF        ormat:@ "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" messag    E:message delegate:nil cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil]; [Alertview show];}        -(ibaction) Search: (ID) Sender {mbconnection *conn = [mbconnection connection]; Requestsuccessblock Successblock = ^ (Mbrequest *request, Mbmetadata *metadata) {mblist *list = metadata.        Releaselist;                Mbrelease *release = [list elementatindex:0]; Album of 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 Quality _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 _resultcontroller.date = (release. Date)? Release.                date:unknownstring; Album release Country _resultcontroller.country = (release. Country)? Release.                country:unknownstring; NSLog (@ "Album barcode =%@", release.        Barcode); NSLog (@ "Asin standard identification code, from excellent Amazon =%@", release.                Asin); Mblist *mediumlist = release.        Mediumlist;                Mbmedium *medium = [Mediumlist elementatindex:1]; Album release Form _resultcontroller.format = Medium.                Format; NSLog (@ "Number of music in album =%d", Medium.                TrackList.Count.integerValue); Use a list to display the specific information for the album [Self Showalldata:release.    ID];        }; Mbrequest *req = [mbrequest searchforentity:mbentityrelease query:@ "Violin concer TOS "//query:@" Don't want to Let go "limit:[nsnumber num BerwITHINTEGER: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 |        Mbincparameterrecordingrels)]; void (^successblock) (Mbrequest *, mbmetadata *) = ^ (Mbrequest *request, Mbmetadata *metadata) {mbrelease *release = metadata.        Release;            if (release) {_resultcontroller.tracks = [Nsmutablearray array]; Mblist *mediumlist = release.            Mediumlist;            Mbmedium *medium = [Mediumlist elementatindex:0]; Mblist *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 IOS (ii) with use MusicBrainz. The only type of search is mbentityrelease. After searching for and getting to the first result in the list, we select its mbid for further queries, because only the query talent through Mbid find out what songs are included in the album.

After the query succeeds. Load the data in a table. Resultviewcontroller class codes such as the following:

#import <UIKit/UIKit.h> @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) N Sstring *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 *sta Tus; @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) {CA        Se 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 information"; 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:@ "Release area:%@", 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 results of the execution are as follows:



The program may continue to change to see what further needs to be proposed tomorrow.


Use MusicBrainz in IOS (iii) query the full information of the album

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.