標籤:音樂 rip 才幹 meta http blank dexp err count
本文討論下通過專輯名擷取專輯的完整資訊。包含歌曲列表。藝術家列表,發行時間和地區等。
因為是通過專輯名搜尋專輯資訊。所以搜尋出來的結果可能較多,比如一個“Violin Concertos”就可能包括多個搜尋結果,而本文僅僅是顯示專輯的完整資訊,並不進行進一步的匹配工作,因此以第一個搜尋結果為例。
代碼例如以下:
#import "AlbumViewController.h"#import "MB.h"#import "ResultViewController.h"#define UnknownString @"未知"#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 localizedDescription]]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:message delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alertView show]; };}- (void)alertWithMessage:(NSString *)message { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:message delegate:nil cancelButtonTitle:@"確定" 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]; // 專輯的mbid _resultController.mbid = (release.Id) ? release.Id : UnknownString; if (!release.Id) { [self alertWithMessage:@"搜尋失敗"]; return; } // 專輯名 _resultController.title = (release.Title) ? release.Title : UnknownString; // 專輯狀態 _resultController.status = (release.Status) ? release.Status : UnknownString; // 專輯音質 _resultController.quality = (release.Quality) ? release.Quality : UnknownString; MBTextRepresentation *textRepresentation = release.TextRepresentation; // 專輯語言 _resultController.language = (textRepresentation.Language) ? textRepresentation.Language : UnknownString; // 專輯劇本語言 _resultController.script = (textRepresentation.Script) ? textRepresentation.Script : UnknownString; MBArtistCredit *artistCredit = release.ArtistCredit; NSUInteger nameCount = (artistCredit.elementCount) ? artistCredit.elementCount : UnknownInteger; // 專輯藝術家列表 _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(@"發行組織類型 = %@", group.Type); NSLog(@"發行組織名稱 = %@", group.Title); // 專輯發行日期 _resultController.date = (release.Date) ? release.Date : UnknownString; // 專輯發行國家 _resultController.country = (release.Country) ? release.Country : UnknownString; NSLog(@"專輯條碼 = %@", release.Barcode); NSLog(@"Asin 標準識別碼,來自卓越亞馬遜 = %@", release.Asin); MBList *mediumList = release.MediumList; MBMedium *medium = [mediumList elementAtIndex:1]; // 專輯發行形式 _resultController.format = medium.Format; NSLog(@"專輯中的音樂個數 = %d", medium.TrackList.Count.integerValue); // 用一個列表顯示出專輯的具體資訊 [self showAllData:release.Id]; }; MBRequest *req = [MBRequest searchForEntity:MBEntityRelease query:@"Violin Concertos"// query:@"不想放手" 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 | 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:@"搜尋失敗"]; } }; [conn enqueueRequest:req onSuccess:successBlock onFailure:self.failureBlock];}@end
其實,這和Use MusicBrainz in iOS(二)中的搜尋方法是一樣的。僅僅是搜尋的類型是MBEntityRelease。在搜尋並擷取到列表中的第一個結果後,我們選取其mbid進行進一步的查詢,由於僅僅有通過mbid的查詢才幹尋找到該專輯中包括什麼歌曲等資訊。
在查詢成功後。用一個表格來載入這些資料。ResultViewController類代碼例如以下:
#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) 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 @"音樂列表"; case 1: return @"藝術家"; case 2: return @"基本資料"; case 3: return @"附加資訊"; 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:@"發行日期:%@", self.date]; break; case 1: text = [NSString stringWithFormat:@"發行地區:%@", self.country]; break; case 2: text = [NSString stringWithFormat:@"發行形式:%@", self.format]; break; case 3: text = [NSString stringWithFormat:@"語言:%@", 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:@"品質:%@", self.quality]; break; case 1: text = [NSString stringWithFormat:@"狀態:%@", self.status]; break; default: break; } cell.textLabel.text = text; } return cell;}@end
執行結果例如以下:
程式可能會繼續改動,看看明天有什麼進一步的需求提出來吧。
Use MusicBrainz in iOS(三)查詢專輯的完整資訊