orgami
orgami

Reputation: 161

How to play music from the ipod in custom app when the app goes to background

I am developing a music app, where the music continues playing even when the app goes into background and uses beginReceivingRemoteControlEvents so that the user can control the status of the playback by clicking the home button twice on his iPhone/iPad.

I went through the "Music App" Sample developed by Apple where it uses MPMusicPlayerController where it fetches songs from iPod Library, but it stops the song as soon as the app goes to background and does not support beginReceivingRemoteControlEvents.

I checked a few articles where it says use AVPlayer, but it doesnt support fetching music from iPod Library so I am stuck in how to play the music when the app goes into background and it is controlled via remoteEvents.

Upvotes: 4

Views: 2173

Answers (1)

Jesse Gumpo
Jesse Gumpo

Reputation: 4797

iPodMusicPlayer will play in the background, but through the iOS iPod Music Player. Your app will not receive notifications in the background. For that, you would need to set up an AVAudioSession and play with AVPlayer.

Check here for more information

Apple has a great resource here

From the Apple Docs:

Use an MPMusicPlayerController object, or music player, to play media items from the device iPod library. There are two types of music player:

The application music player plays music locally within your application. It is not aware of the iPod app’s now-playing item, nor does it affect the iPod state.

The iPod music player employs the built-in iPod app on your behalf. On instantiation, it takes on the current iPod application state, such as the identification of the now-playing item. If a user switches away from your app while music is playing, that music continues to play. The iPod app then has your music player’s most recently-set repeat mode, shuffle mode, playback state, and now-playing item.

Make sure you are using

+ iPodMusicPlayer

and not

+ applicationMusicPlayer

Here is a good article on how to import the library into an AVAudioSession

Add a row to your info.plist file with the name "UIBackgroundModes" Enter "audio" as the first key.

UIBackgroundModes

Upvotes: 4

Related Questions