I'm programming an application for the hearing-impaired. I'm hoping to take tracks from the iTunes library, and in my app have a slider for panning. I don't want to use OpenAL (this isn't a game - I repeat this is a media player). So since AVAudioPlayer has the easy pan method, can I take selections from the MPMediaPicker and feed them into the AVAudioPlayer so I can pan them?
2 Answers
I dont do a lot of iOS development, but I believe there are two ways.
Method #1 You need to add /System/Library/Frameworks/AVFoundation.framework to your target in Xcode and #import AVAudioPlayer.h as well as You need to add MediaPlayer.framework to your target in Xcode and #import .
For this operation, you need MPMediaPicker to pass the song data to AVAMedia Player. That can be accomplished like this:
@interface MusicPlayerDemoViewController : UIViewController <MPMediaPickerControllerDelegate> {
...
}
...
// This action should open the media picker
- (IBAction)openMediaPicker:(id)sender;
@end
// MusicPlayerDemoViewController.m
- (IBAction)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];
}
// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
(Code Continues Below, the blanks are for you to fill in)
AT THIS POINT, CALL THE AVAAUDIOPLAYER CLASS AND TELL IT TO PLAY mediaItemCollection . REMEMBER TO STOP AUDIO BEFORE PLAYING, AS IT WILL PLAY MULTIPLE SONGS AT ONCE.
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// User did not select anything
// We need to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
}
NOW, ONCE THIS IS DONE THE USER NEEDS TO SELECT A NEW SONG. YOU COULD EITHER CREATE A WHILE LOOP AROUND THE WHOLE THING, WHERE THE CONDITIONAL IS CURRENT TIME >= DURATION (FROM AVAAUDIO PLAYER),
ALTERNATIVELY, YOU COULD CREATE A BUTTON TO OPEN THE PICKER
For more questions checkout:
http://oleb.net/blog/2009/07/the-music-player-framework-in-the-iphone-sdk/ (I used much of their code)
Good Luck!
Derek

- 21
- 1
-
Thanks. I think I really should do a custom class. My idea was to take the picked audio files and make an alias of them in a sandboxed location in the app. That way the AVAudioPlayer can call them as it normally would, and play them. So the trouble I have now is that I can't use stuff from the ViewController.h in the custom class! Do I need to make the class inherit from the ViewController? – ThurnisH41ey Jul 17 '11 at 15:10
-
Ah-I think I got it. Add a #import statement in the custom class that imports the view controller. But now, how do I make an alias of the MPMediaPicker selection? – ThurnisH41ey Jul 17 '11 at 15:12
-
Nope this all isn't working. I can't figure out the play methods of AVAudioPlayer, it's weird. I do have a pan slider up and running, but I'm still lost on how to use the MPMediaPicker selection in the AVAudioPlayer. – ThurnisH41ey Jul 17 '11 at 15:48
-
I have a bigger problem now. I found the URL of the media item using MPMediaItemPropertyAssetURL, but now I have a bigger problem. When the media picker loads: 2011-07-18 23:06:45.159 PeterPan[301:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PeterPanViewController handle_VolumeChanged:]: unrecognized selector sent to instance 0x25c790' – ThurnisH41ey Jul 19 '11 at 03:08
Try having AVAMediaPlayer play the variable mediaItemCollection. This was assigned by the picker to be the song location by the code above. If this does not work make sure that AVAMediaPlayer uses the same input variable type (format, like an ID or a folder location) as MpMediaPicker.
That error message sounds like a technical issue. The only thing I can think of is that AVAAudio player or MPmedia player is looking for a Volume variable (it is required?) and can't find one. I can't really answer this one as I don't do iPhone Development, try there forums or website for some help.
Sounds like you are doing a good job! If you are interested, (I don't know if you are staying at DA) Mr. Cochran (the dean of students at the Upper School) is teaching a iPhone Development Class and a AP Computer Science Class (I am in). If you want to take it further, or you want to just ask questions I know he is more than happy too!
Good Luck! Tell me when it is finished so I can test the results!

- 21
- 1
-
-
Yes-it's a required method. However, now, when I select a song, the media picker doesn't go away and nothing plays. This is turning into a much bigger pain that it should be. And yes, I'm staying at DA. – ThurnisH41ey Jul 19 '11 at 18:16
-
Is there an AVAMediaPlayer? or did you mean AVAudioPlayer? And yes, that's what I'm doing. - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection { if (mediaItemCollection) { [musicPlayer setQueueWithItemCollection:mediaItemCollection]; panPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[mediaItemCollection valueForProperty:MPMediaItemPropertyAssetURL] error:nil]; [panPlayer prepareToPlay]; [panPlayer play]; [musicPlayer play]; } – ThurnisH41ey Jul 21 '11 at 21:29
-
[self dismissModalViewControllerAnimated: YES]; } That's what happens when you pick a song. – ThurnisH41ey Jul 21 '11 at 21:29