在我为Apple设计的示例应用程序中,无法显示MPMediaPickerController。
我已经在info.plist文件中添加了NSAppleMusicUsageDescription键,这使我的帖子与在线找到的其他答案有所不同。
我还尝试添加CoreMedia.framework。
我正在使用XCode 10 beta 4
该应用程序从不要求访问媒体库的权限,当我显示选择器时,会立即调用mediaPickerDidCancel方法。
我在做什么错了?
感谢您的帮助!
头文件:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMediaPickerController.h>
@interface ViewController : UIViewController<MPMediaPickerControllerDelegate>
@end
obj文件:
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@end
@implementation ViewController {
MPMediaPickerController *picker;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)selectButtonPressed:(id)sender {
picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];
[picker setDelegate: self];
[picker setAllowsPickingMultipleItems: NO];
picker.prompt = @"Select a Song.";
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootController presentViewController:picker animated:YES completion:^{
NSLog(@"Complete!");
}];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection {
MPMediaItem *firstItem;
for (MPMediaItem *item in collection.items) {
firstItem = item;
break;
}
MPMusicPlayerController *samplePlayer = [MPMusicPlayerController applicationMusicPlayer];
[samplePlayer setShuffleMode: MPMusicShuffleModeOff];
[samplePlayer setRepeatMode: MPMusicRepeatModeOne];
[samplePlayer beginGeneratingPlaybackNotifications];
// self.mediaItem chosen using MPMediaPickerController
[samplePlayer setQueueWithItemCollection:[[MPMediaItemCollection alloc] initWithItems:@[firstItem]]];
[samplePlayer prepareToPlay];
// Assume that song is at least 120 seconds long
[samplePlayer play];
}
#pragma mark - delegate methods and segues
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {
[self dismissViewControllerAnimated:true completion:^{
}];
}
@end