There are at least four different ways of playing audio in iOS. Each has their own wrinkles and advantages. In this article I briefly compare and contrast some of the differences between the options. For the TL;DR version skip to the table at the end which compares the approaches. (actually, to be honest, I’ve had this post in my drafts for a very long time, and just wanted to get it published. The body of the article isn’t as good as the table — skim the article and go to the table )
- MPMusicPlayerController(using the iPodMusicPlayer)
- MPMusicPlayerController(using the applicationMusicPlayer)
The MPMusicPlayerController using the iPodMusicPlayer is the highest level player, and matches closely to the iPod. The actual iPod music player can be accessed and used with the factory method:
The nowPlaying item property will expose the current playing item in the iPod when using the iPodMusicPlayer. Changes to the current item in your application will also update the main iPod music player.
To load your own music player with the same API, use:
In iOS 5.1 and below it is not easily possible to use the AVAudioPlayer to play items from the iTunes library. While it is possible to retrieve the url for the object, it doesn’t really do what you want. When trying this you will see a somewhat unhelpful error:
Domain = NSOSStatusErrorDomain
Code = -43
Description = Error Domain=NSOSStatusErrorDomain Code=-43 “The operation couldn’t be completed. (OSStatus error -43.)”
The AVPlayer however, allows access to the iTunes library, playing in the background, custom background behaviour, and playing non-Library audio.
Of the accesible API’s this is the most functionality. For the hard-core/determined developer, there are lower levels to dig down into, but these 4 options provide a decent set of options to play audio in iOS. The table below outlines the highlights.
|access iPod play location||Y||N||N||N|
|Play in Background||Y||N||?||Y|
|Custom Background behaviour||N||N/A||?||Y|
|Play non-Library Audio||N||N||Y||Y|
No related posts.