在iOS模拟器中使用MPMediaPickerController时出现运行时错误

时间:2012-05-29 02:30:24

标签: ios ios-simulator mpmediapickercontroller

当我尝试使用iOS模拟器上的MPMediaPickerController运行应用时会发生以下情况。

2012-05-28 22:26:42.416 My App[48426:11f03] Could not load source: 3
2012-05-28 22:26:42.418 My App[48426:11f03] *** Assertion failure in -[MPMediaPickerController loadView], /SourceCache/MediaPlayer_Sim/MobileMusicPlayer-1391.72/SDK/MPMediaPickerController.m:86
2012-05-28 22:26:42.419 My App[48426:11f03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to load iPodUI.framework'

这是我的App / Xcode / iOS模拟器中的一些问题,还是iOS模拟器根本不支持MPMediaPickerController?如果没有,除了在物理设备上运行之外还有其他选择吗?

3 个答案:

答案 0 :(得分:14)

MPMediaPickerController在模拟器中不起作用。 Apple在“Hello Music Player”下的“iPod Library Access Programming Guide”中注明了这一点。说明说:

  

注意:要执行这些步骤,您需要配置设备,因为   模拟器无法访问设备的iPod库。

为了防止断言,您可以随时检查是否可以在代码中访问(请使用ARC和iOS SDK 5.0代码)。

MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];

[picker setDelegate:self];
[picker setAllowsPickingMultipleItems:YES];
[picker setPrompt:NSLocalizedString(@"Add songs to play","Prompt in media item picker")];

@try {
    [picker loadView]; // Will throw an exception in iOS simulator
    [self presentViewController:picker animated:YES completion:nil];
}
@catch (NSException *exception) {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Oops!",@"Error title")
                                message:NSLocalizedString(@"The music library is not available.",@"Error message when MPMediaPickerController fails to load") 
                               delegate:nil 
                      cancelButtonTitle:@"OK" 
                      otherButtonTitles:nil] show];
}

答案 1 :(得分:2)

另外(如果使用故事板)你可以试试:

- (IBAction)showPicker:(id)sender
{
#if TARGET_IPHONE_SIMULATOR
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"playerTest"
                                                    message:@"Media picker didn't work in simulator, please run this app on device"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
#else
    [self performSegueWithIdentifier:@"ShowPickerViewSegue" sender:self];
#endif
}

答案 2 :(得分:0)

MPMediaPickerController现在可以在iOS模拟器中运行而无需任何额外的代码更改(至少从iOS 8开始,可能更早)。这是一个可以演示它的项目:GVMusicPlayerController

您必须通过从实际设备复制必要的文件(最重要的是MediaLibrary.sqlitedb数据库文件)来在Simulator中准备音乐库。如果您要播放文件和查看图片,则还必须复制iTunes_Control/MusicPurchasesArtwork文件夹(位于/var/mobile/Media/中)。有关详细信息,请参阅此问题:Can i access iPod Library on simulator?