I found myself in a situation where I need to simulate audio playback to trick OS controls and MPNowPlayingInfoCenter
into thinking that an audio is being played. This is because I am building a player that plays multiple audio tracks, with pauses in-between creating one, continuous "audio" track. I have already everything setup inside the app itself, and the lock screen controls are working correctly but the only problem I am facing is while the actual audio stops and a pause is being "played", the lock screen info center stops the timer, and it only continues with showing correct time and overall state once another audio track starts playing.
Here is the example of my audio track built from audio files and pause items:
let items: [AudioItem] = [
.audio("part-1.mp3"),
.pause(duration: 5), // value of type: TimeInterval
.audio("part-2.mp3"),
.pause(duration: 3),
... // the list goes on
]
then in my custom player, once AVAudioPlayer
finishes its job with current item, I get the next one from the array and play either a .pause
with a scheduled Timer
or another .audio
with AVAudioPlayer
.
extension Player: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
playNextItem()
}
}
And here lies the problem, once the AVAudioPlayer
stops, the Now Playing info center automatically stops too, even tho I keep feeding it fresh nowPlayingInfo
. Then when it hits another .audio
item, it resumes correctly and shows current time, etc.
And here lies the question
how do I trick the MPNowPlayingInfoCenter
into thinking that audio is being played while I "play" my .pause
item?
I realise that it may still not be clear, what I am trying to achieve but I am happy to share more insight if needed. Thanks!
Some solutions I am currently thinking about:
A. Keeping 1s long empty audio track that would play on loop for as long as the pause is needed to play.
B. Creating programatically empty audio track with appropriate lenght and playing it instead of using Timer
for keeping track of pause duration/progress and relying completely on AVAudioPlayer for both .audio
and .pause
items. Not sure this is possible though.
C. Maybe there is a way to tell the MPNowPlayingInfoCenter
that the audio keeps playing without the need of using AVAudioPlayer
but some API I am not familiar with?