I'm currently developing an app that can reproduce audio and video from the web (my own server) using SwiftUI targeting iOS15 and testing on a iPhone XS Pro Max running iOS16.2 (real device)
Both the video and audio play just fine but I get this (image in the end of the post) in as the media controls. The current time and duration are correct and updated (without me calling to update them) but I'm unable to actually update the title, artist, album and artwork
My first thought was to update the NowPlaying and after a lengthy search I'm yet to find a solution that works
I created a small class to update the NowPlaying info
import Foundation
import UIKit
import MediaPlayer
public class MediaController {
var nowPlayingInfo = [String: Any]()
static let shared = MediaController()
private init() { }
func setupNotificationView(title: String?, album: String?, artist: String?, thumbnailUrl: String) {
nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = title
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = album
nowPlayingInfo[MPMediaItemPropertyArtist] = artist
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
}
The NowPlaying still doesn't update
This is the call sequence when I press play
let avAsset = AVURLAsset(url: URL(string: "https://domain.com/video.mp4")
let avItem = AVPlayerItem(asset: avAsset)
player.replaceCurrentItem(with: avItem)
do {
try AVAudioSession.sharedInstance().setActive(true);
} catch { print(error) }
player.play()
MediaController.shared.setupNotificationView(title: "A Title", album: "A Album", artist: "A Artist", thumbnailUrl: "")
Although the app is being developed using SwiftUI I'm using the UIViewControllerRepresentable to display a the video using AVPlayerViewController
since the current one misses the PiP
and under makeUIViewController
I have the following code
let controller = AVPlayerViewController()
controller.player = player
controller.allowsPictureInPicturePlayback = true
controller.entersFullScreenWhenPlaybackBegins = true
controller.delegate = context.coordinator
and on the app launch I have the following
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playback)
} catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
And yes I have the background mode capability enabled for "Audio, AirPlay and Picture in Picture"
I'm at a lost here and hope someone can help.