Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS iPhone use MPMusicPlayerController to play external music file, and display iPod interface

Tags:

ios

audio

I'm trying to use the standardized "iPod" audio player to play some MP3 tracks in an iPhone app I'm building. The tracks are downloaded from the internet and stored in the app's "Documents" directory. I thought of using a MPMusicPlayerController to do this, but I don't seem to be able to get it to work. Also, I've seen the AVAudioPlayer, but that just plays the audio without an interface. Any suggestions?

like image 331
user974771 Avatar asked Nov 27 '25 01:11

user974771


1 Answers

The MPMusicPlayerController is for playing items out of the iPod library (songs sync'd via iTunes) so you won't be able to use it for this.

You can get the NSData for your audio using...

NSData* data = [NSMutableData dataWithContentsOfFile:resourcePath options:0 error:&err];

Then use an AVAudioPlayer created from that data and call play.

AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithData:data error:&err];
[player play];
like image 185
TurqMage Avatar answered Nov 28 '25 15:11

TurqMage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!