new to Objective-C but I'm trying to add a music picker to my app in xCode, the thing where you click it and it opens up the users music library for them to pick a song, but I can't find out how to do this and all documentation i've seen doesn't seem to work. For example,
MPMediaPickerController *picker =
[[MPMediaPickerController alloc]
initWithMediaTypes: MPMediaTypeAnyAudio]; // 1
[picker setDelegate: self]; // 2
[picker setAllowsPickingMultipleItems: YES]; // 3
picker.prompt =
NSLocalizedString (@"Add songs to play",
"Prompt in media item picker");
[myController presentModalViewController: picker animated: YES]; // 4
[picker release];
is directly from Apple's help documentation. When I type this into my app I get the errors:
mediaPicker.delegate = self; // Error: Assigning to 'id<MPMediaPickerControllerDelegate?' from incompatible type "ViewController *const__stron"
and
[self presentModalViewController:mediaPicker animated:YES]; // Error: 'presentModalViewController:animated:' is deprecated: first deprecated in iOS 6.0
I feel like I need to do something with the mediaPicker.delegate = self;
line but no idea what. Any help?
Change this line:
mediaPicker.delegate = self; // Error: Assigning to 'id<MPMediaPickerControllerDelegate?' from incompatible type "ViewController *const__stron"
to
mediaPicker.delegate = id<MPMediaPickerControllerDelegate>self;
or you can go the Header file of the self class and there conform to the MPMediaPickerControllerDelegate
by writing it as so --
@interface CLassName : UIViewController <MPMediaPickerControllerDelegate> {
//code
}
As for your second problem use this method:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0)
the method you are using has been deprecated, so better use this new one. Use this as such:
[self presentViewController:mediaPicker animated:YES completion:nil];
You can change animated to NO
, if you want, as for the completion block. It is there so that in case if you need to perform some operations after the completion of the animation, you can write your code in this block.
Hope this helps. Cheers.
*****EDIT*****
Suppose your class name is TestMediaPickerController
. Then go to TestMediaPickerController.h
file, then there in the header file where you have declared the interface, do this:
First add this to header.
#import <MediaPlayer/MediaPlayer.h>
then
@interface TestMediaPickerController : UIViewController <MPMediaPickerControllerDelegate> { //Adding this `<MPMediaPickerControllerDelegate>` here makes your TestMediaPickerController Class confirm to the Protocol MPMediaPickerControllerDelegate.
//code
}
The Problem that you are having it seems you are having trouble setting up the MPMediaPickerControllerDelegate.