How to browse and Play local audio files on an iOS Device?
I am developing an iPhone app in which I need to provide the facility to the user where he/she will be able to select开发者_如何转开发 audio files saved on the device and will be able to play them in my application.Please give me right direction to proceed. Thanks
you'll have to use MPMediaPickerControllerDelegate for this.
//in your .h file include the MPMediaPickerControllerDelegate
//in your .m file
- (IBAction) showMediaFilesPressed : (id) sender
{
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString(@"AddSongsPrompt", @"Prompt to user to choose some songs to play");
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault animated:YES];
[self presentModalViewController: picker animated: YES];
[picker release];
}
This will give you the all the media files listed on your device. And to play those files you may write the code for that in its delegate method.
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
//play your file here
}
This article might help you more. http://oleb.net/blog/2009/07/the-music-player-framework-in-the-iphone-sdk/
Just a little but important aside: You will not be able to test run this code on the iOS simulator, since the simulator is not loaded with a media library.
The AddMusic sample project therefore has this statement in its project code:
#pragma mark Application setup____________________________
#if TARGET_IPHONE_SIMULATOR
#warning *** Simulator mode: iPod library access works only when running on a device.
#endif
精彩评论