Objective-C (iOS): iPod ライブラリから音楽を再生

MediaPickerViewController

MediaPlayer.framework をリンクしてください。

Objective-C: MediaPickerViewController.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MediaPickerViewController : UIViewController <MPMediaPickerControllerDelegate>

@end

Objective-C: MediaPickerViewController.m

#import "MediaPickerViewController.h"

@interface MediaPickerViewController ()

@end

@implementation MediaPickerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 200, 50);
    [button setTitle:@"音楽ライブラリを開いて再生" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonWasTapped:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonWasTapped:(UIButton *)button
{
    MPMediaPickerController *picker = [[MPMediaPickerController alloc]
                                       initWithMediaTypes:MPMediaTypeAnyAudio];
    [picker setDelegate: self];
    [picker setAllowsPickingMultipleItems: YES];
    picker.prompt = NSLocalizedString (@"Add songs to play", "Prompt in media item picker");
    [self presentViewController:picker animated:YES completion:nil];
}

- (void) mediaPicker:(MPMediaPickerController *) mediaPicker
   didPickMediaItems:(MPMediaItemCollection *) collection {
    MPMusicPlayerController *mediaPlayer = [[MPMusicPlayerController alloc] init];
    [mediaPlayer setQueueWithItemCollection:collection];
    [mediaPlayer setRepeatMode:MPMusicRepeatModeAll];
    [mediaPlayer play];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void) mediaPickerDidCancel:(MPMediaPickerController *) mediaPicker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end