Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - What's the right audio player for my app?

I'm designing a music app that plays music from the user's iPhone music library and I'm having problems figuring out which audio player is the right one to use for it (AVAudioPlayer, AVPlayer, or MPMusicPlayer).

My app needs to do the following:

  • Play music from the iPhone music library
  • Control app music volume separately from device/system volume
  • Continue to play app music when app moves to background
  • Catch events when song changes to next song or finishes

From my research it seems like each of the three audio players mentioned above do SOME of the tasks required for my app, but none of them do ALL of them. AVPlayer seems to get the closest, except its volume is dependent on the device/system volume.

Does anyone have any recommendations or workarounds to accomplish this? I've been wracking my brain over this for quite some time so any help at all would be appreciated.

like image 896
md-aintright Avatar asked Nov 13 '22 02:11

md-aintright


1 Answers

EDIT

The MPMusicPlayerController class does not actually support playing background audio when getting an instance of it via + applicationMusicPlayer. The best option, then, is to simply use MPMusicPlayer to query the iPod music library. Once a song is selected by the user, the resulting MPMediaItem can be queried for it's asset URL and fed into AVPlayer's +playerWithURL, giving you full control of playback parameters.

--

I would recommend MPMusicPlayerController.

  1. MPMusicPlayerControllerhandles the low-level details of playing audio files in the iTunes library.
  2. You can use the class method -applicationMusicPlayer to get a local copy of the iPod singleton; from there, you can control its volume by setting its volume property for just your application.
  3. I would assume MPMusicPlayerController has background audio support already built in. If it doesn't, you can change your app's audio session context to make it work (see Playing background audio in http://www.sagorin.org/2011/11/29/ios-playing-audio-in-background-audio/)
  4. You can setup any object you choose to be a KVO observer of your application-specific MPMusicPlayerController object. This way, you can be notified whenever the nowPlayingItem property is changed.

Hopefully this high-level description suffices. Let me know if you need any further clarification.

like image 71
Dany Joumaa Avatar answered Dec 17 '22 20:12

Dany Joumaa