I have developed an iOS audio playback application using SwiftUI, AVFoundation, MediaPlayer, and ActivityKit. The PlayAudioService class handles audio playback, including background play, lock screen controls, and updating the now playing information for the lock screen and control center.
The app is capable of playing audio in the background successfully. However, despite correctly setting up the MPNowPlayingInfoCenter with the necessary keys (title, artist, artwork, duration, and elapsed time), the lock screen and control center are not displaying the correct playback information or controls. Through logging, I can confirm that my updateNowPlayingInfo() method is being called and the MPNowPlayingInfoCenter.default().nowPlayingInfo dictionary contains the expected values. Despite this, these updates do not reflect on the user interface.
Here’s a relevant snippet from my PlayAudioService class where I update the now playing info:
func updateNowPlayingInfo() {
print("Updating playback info")
DispatchQueue.main.async {
if let player = self.player {
var nowPlayingInfo = [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = self.trackTitle
nowPlayingInfo[MPMediaItemPropertyArtist] = "Artist Name"
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.duration
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime
if let albumArt = self.albumArt {
let artwork = MPMediaItemArtwork(boundsSize: albumArt.size) { _ in
return albumArt
}
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
}
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
print(MPNowPlayingInfoCenter.default().nowPlayingInfo ?? "No Now Playing Info")
}
}
}
When I log the MPNowPlayingInfoCenter.default().nowPlayingInfo, it prints out the following information, indicating that the data is set correctly:
[
"MPNowPlayingInfoPropertyElapsedPlaybackTime": 51.41918367346939,
"artist": "Artist Name",
"artwork": <MPMediaItemArtwork: 0x3039f7560>,
"playbackDuration": 728.6668027210884,
"title": "84ABB1F1-AA30-45A1-BEE9-B22363EE79D2.mp3"
]
What could be causing the lock screen and control center to not display the correct playback information even though MPNowPlayingInfoCenter seems to be updated properly? Are there any common pitfalls or additional settings I might have missed?
Mango Cat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.