All of the threads on this topic are really old (5+ yo), and I can't seem to form a clear MODERN approach, so hopefully this can be summarized for 2020 (Swift 5, Xcode 11).
If you were to build an AUDIO (not just music) player app for the iPhone with iOS 13 in a way that is forward thinking (don't care about anything that won't support iOS 14 - so nothing below 8), what Kits/Classes would you use if the requirements are...
BONUS (if possible yet):
So would you use AVAudioPlayer or AVPlayer or MPMusicPlayerController? MPRemoteCommandCenter or MPNowPlayingInfoCenter?
Perhaps some of the requirements are mutually exclusive, or even impossible, but I can't understand why there are so many methods to control audio playback, with no clear delineation between their use cases, or why it matters if the audio is music or speech, downloaded or streamed. Audio is obviously a second class citizen to video at Apple, especially speech, but I would think there should be some direction by now on certain types of basic applications like playlists, podcasts and other types of audio applications.
I'll try answer your question to the best of my knowledge:
App can store and play downloaded MP3 files LOCALLY (not necessarily streamed or embedded).
AVPlayer
or AVAudioPlayer
.
AVPlayer
has more capabilities.
Check Apple's documentation here for AVPlayer
.
And here for AVAudioPlayer
.User has basic player controls over the audio (play/pause/scrub/volume).
AVAudioPlayer
has all the required controls over the played sound file.App has an audio queue (order of track play) that can be adjusted at any time by the app.
AVQueuePlayer
which inherits from AVPlayer
.
Check apple's docs here.User can select to play on bluetooth audio devices phone is paired to.
AVAudioSession
options.
Check docs here.The audio can play in the background (when screen is off, or in another app).
BackgroundMode
capability to you app and check audio, airplay options.The audio can trigger a track and adjust queue in the background (without user interaction).
The audio integrates with native iOS controls (RemoteControlEvents).
MPNowPlayingInfoCenter
and update its info to be able to update the song title, artist, artwork, etc... Apple's docs here.In addition to MPRemoteCommandCenter
to listen to events and update your player accordingly docs.
App audio can overlay playing music as a directions app does (I realize this negates the background and "now playing" features, but perhaps it aids the discussion for clearer reasoning to selecting a method for app developers.
AVAudioSession's
category options, add .mixWithOthers
option.So would you use AVAudioPlayer or AVPlayer or MPMusicPlayerController? MPRemoteCommandCenter or MPNowPlayingInfoCenter?
Use MPMusicPlayerController
if you playing from the device library.
I think the above summarize the highlights of it. There are many things you should care about when you using these things together not all options works with all AVAudioSession's
modes, for example.
There are many properties inside the Players, and its setting and the wrong combination might prevent your player from working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With