I am working on an IOS app that after some interactions with the user and some timer finishes running, I am playing music from the music library. It was working fine with local music and audiobooks (books app) and third party music players. I am testing this by playing some music from another app (Music, Books, etc...) and seeing if my app will replace what is being played when triggered or not and play music from my app, it does.
Strangely I started testing today with the podcast app and it is not working consistently, sometimes it stops the podcast playing and sometimes it does not, the device just ignores musicPlayer.play() and keeps the podcast going.
I tried the three variations of media players
let applicationMusicPlayer = MPMusicPlayerController.applicationMusicPlayer
let systemMusicPlayer = MPMusicPlayerController.systemMusicPlayer
let applicationQueuePlayer = MPMusicPlayerController.applicationQueuePlayer
let mediaItems = MPMediaQuery.songs().items
if mediaItems != nil {
let mediaCollection = MPMediaItemCollection(items: mediaItems!)
applicationMusicPlayer.setQueue(with: mediaCollection)
systemMusicPlayer.setQueue(with: mediaCollection)
applicationQueuePlayer.setQueue(with: mediaCollection)
applicationMusicPlayer.prepareToPlay()
systemMusicPlayer.prepareToPlay()
applicationQueuePlayer.prepareToPlay()
applicationMusicPlayer.play()
systemMusicPlayer.play()
applicationQueuePlayer.play()
}
I tried each separately, I tried them even together :D and still it is not reliable, sometimes it works sometimes it does not. I am keeping my app opened on the foreground and preventing screen lock using UIApplication.shared.isIdleTimerDisabled = true and I release it at a later stage
there is a "very ugly" hack that I managed to do which works reliably
var x = 50
while x > 1 {
applicationQueuePlayer.play()
x -= 1
}
Please advise if there is a way to priorities the now playing item of the device ? like give my app a higher priority or something so it can force the phone to stop whatever else is playing and play music through my app
One more note: Sometimes I get this error right after I initialize the media player
[SDKPlayback] applicationQueuePlayer _establishConnectionIfNeeded timeout [ping did not pong]
[core] Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)
[iTunesCloud] [ICUserIdentityStore] Failed to fetch local store account with error: Error Domain=com.apple.accounts Code=9
I looked around online and it seems a bug with IOS 13 but not sure if it is related or not (I sometimes get it when it works or when it does not work)
info:
- Xcode: Version 11.4.1 (11E503a)
- Device: iPhone8,4 (iPhone SE)
- Device IOS: 13.4.1 build 17E262
- Deployment IOS: 13.3