在iOS模拟器中使用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 ? 如果没有,除了在物理设备上运行,还有其他的select吗?

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

注意:要执行这些步骤,您需要configuration设备,因为模拟器无法访问设备的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]; } 

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

 - (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 } 

MPMediaPickerController现在可以在iOS模拟器中工作,不需要任何额外的代码改变(至less从iOS 8开始,可能更早)。 这是一个可以演示的项目: GVMusicPlayerController 。

您必须通过从实际设备(最重要的是MediaLibrary.sqlitedb数据库文件)复制必要的文件来准备模拟器中的音乐库。 如果您想播放文件并查看图片,则还需要复制iTunes_Control/MusicPurchasesArtwork文件夹(可在/var/mobile/Media/ )。 看到这个问题的进一步细节: 我可以访问模拟器上的iPod库吗? 。