Pergunta

Hi I'm using MPMediaPickerController to picking Audio file from Phone library. but it give me blank screen. I can't understand why this is happening. I'm running app on simulator. This my code

- (IBAction)selectFile:(UIButton *)sender{

MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES;
mediaPicker.prompt = @"Select Your Favourite Song!";
[mediaPicker loadView];
[self.navigationController presentViewController:mediaPicker animated:YES completion:nil]; }
Foi útil?

Solução 2

MPMediaPickerController does not work in the Simulator. Apple notes this in the "iPod Library Access Programming Guide" under "Hello Music Player". The note says:

Note: To follow these steps you’ll need a provisioned device because the Simulator has no access to a device’s iPod library.

To prevent the assertion you can always check if you can access the do this in your code (code bellow uses ARC and 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];
}

Outras dicas

For those who want the answer in Swift:

@IBAction func openMediaPickerController(sender: AnyObject) {
        let picker = MPMediaPickerController()
        picker.delegate = self
        picker.allowsPickingMultipleItems = true
        picker.prompt = NSLocalizedString("Chose audio file", comment: "Please chose an audio file")
        self.presentViewController(picker, animated: true, completion: nil)
}
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
    self.dismissViewControllerAnimated(true, completion: nil)
}
func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    //run any code you want once the user has picked their chosen audio
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top