你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Use MusicBrainz in iOS(三)查詢專輯的完整信息

Use MusicBrainz in iOS(三)查詢專輯的完整信息

編輯:IOS開發綜合

本文討論下通過專輯名獲取專輯的完整信息,包括歌曲列表,藝術家列表,發行時間和地區等。

由於是通過專輯名搜索專輯信息,所以搜索出來的結果可能較多,例如一個“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 

@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


運行結果如下:

\


程序可能會繼續修改,看看明天有什麼進一步的需求提出來吧。


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved