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.
这是根据音频文件和暂停项目构建的音频轨道的示例:
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.
问题就在这里
how do I trick the MPNowPlayingInfoCenter
into thinking that audio is being played while I "play" my .pause
item?
我意识到可能尚不清楚我要实现的目标,但很高兴在需要时分享更多见解。谢谢!
我目前正在考虑的一些解决方案:
A.保持1s较长的空音频轨道,只要需要播放暂停,它就会循环播放。
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?