Can MPMusicPlayerController be used to play a local audio file?
-
26-06-2021 - |
Question
Can MPMusicPlayerController
be used to play a local audio file i.e. one that is within my app's bundle as opposed within the iPod? I've looked at the documentation and past postings but doesn't look like it can.
If not what is the recommended class for doing so?
Solution
I don't believe so, MPMusicPlayerController
is used for playing MPMediaItems
and MPMediaItemCollections
generated from songs in the users iPod library.
From the docs:
A media item collection is a sorted set of media items (instances of the MPMediaItem class) from the iPod library. Typically, you use this class by requesting an array of collections from a media query by way of its collections property. Media queries are described in MPMediaQuery Class Reference.
AVAudioPlayer
may better suit you, here's an example of how to use AVAudioPlayer
to play a file within your application bundle.
#import <AVFoundation/AVFoundation.h>
Be sure to include to use delegate methods.
AVAudioPlayer *data = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"nameOfSound" ofType:@"mp3"]] error:NULL];
data.delegate = self;
data.volume = 1.0;
[data prepareToPlay];
[data play];