您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

在 iOS 中使用 MusicBrainz(三):查詢專輯的完整資訊

編輯: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

事實上,這和〈在 iOS 中使用 MusicBrainz(二)〉中的搜尋方法一樣,只是搜尋的型別是 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

運行結果如下:

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