オーディオブックのアルバムの一覧を表示してみる
Audio帳(仮)を作るために、iPodのオーディオブックへのアクセスを試してみる。
とりあえず、TableViewにオーディオブックの一覧を表示をやってみる。
まず前提として
- 「MediaPlayer.framework」ライブラリをリンクしていること
TableViewControllerを継承したControllerを作る。アルバムのコレクションを維持するNSArrayを持っておく。
|
1 2 3 4 5 6 7 8 9 10 |
#import <UIKit/UIKit.h>
@interface AlbumViewController : UITableViewController {
@private
NSArray *_albums;
}
@property(nonatomic, retain) NSArray *albums;
@end |
MediaPlayer.hをimportして、MPMediaQueryを利用してアルバムの一覧をゲットする関数を作っておく。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#import <MediaPlayer/MediaPlayer.h>
@implementation AlbumViewController
@synthesize albums=_albums;
- (void)updateAlbums
{
// オーディオブックのクエリのみをゲット
MPMediaQuery *query = [MPMediaQuery audiobooksQuery];
// アルバムの一覧が欲しいので、グルーピングはアルバムにする
[query setGroupingType: MPMediaGroupingAlbum];
// コレクションをとっとく
self.albums = [query collections];
} |
あとはアルバムをTableViewに表示だけ。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// セクションは1で固定しとく
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// アルバム数を返す
return self.albums.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// サブタイトル付きのセル
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// アルバムをげっと
MPMediaItemCollection *album = [self.albums objectAtIndex:indexPath.row];
// アルバム情報
MPMediaItem *representativeItem = [album representativeItem];
// タイトルを表示
cell.textLabel.text = [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
if ([cell.textLabel.text isEqualToString:@""]) {
cell.textLabel.text = @"不明なアルバム";
}
// アーティストを表示
cell.detailTextLabel.text = [representativeItem valueForProperty: MPMediaItemPropertyArtist];
if ([cell.detailTextLabel.text isEqualToString:@""]) {
cell.detailTextLabel.text = @"不明な作成者";
}
return cell;
} |
こんな感じになった。

DUO3.0を例文毎に分割したものもあるのでDUO3.0のアルバムが二つある。
アルバムやアーティストが設定されていないオーディオは空になっているようなので、それは「不明な〜」に置き換えて表示しておく。