HomeiOS Developmentios - SwiftUI Image in Image (PiP) for customized view

ios – SwiftUI Image in Image (PiP) for customized view


I did some finding out on Image in Image for AVPlayerViewController. I am attempting to make it in order that I can put some textual content as subtitles or another Views to the PiP window when the app goes into background however no luck of getting any answer for that. Is there any approach or any work round I can obtain that?

closing class AVPlayerViewModel: ObservableObject {

    @Revealed var pipStatus: PipStatus = .undefined
    @Revealed var media: Media?

    let participant = AVPlayer()
    non-public var cancellable: AnyCancellable?
    
    init() {
        setAudioSessionCategory(to: .playback)
        cancellable = $media
            .compactMap({ $0 })
            .compactMap({ URL(string: $0.url) })
            .sink(receiveValue: { [weak self] in
                guard let self = self else { return }
                self.participant.replaceCurrentItem(with: AVPlayerItem(url: $0))
            })
    }
    
    func play() {
        participant.play()
    }
    
    func pause() {
        participant.pause()
    }
    
    func setAudioSessionCategory(to worth: AVAudioSession.Class) {
        let audioSession = AVAudioSession.sharedInstance()
        do {
           attempt audioSession.setCategory(worth)
        } catch {
           print("Setting class to AVAudioSessionCategoryPlayback failed.")
        }
    }
}

struct AVVideoPlayer: UIViewControllerRepresentable {
    @ObservedObject var viewModel: AVPlayerViewModel
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let vc = AVPlayerViewController()
        vc.participant = viewModel.participant
        vc.canStartPictureInPictureAutomaticallyFromInline = true
        vc.delegate = context.coordinator
        return vc
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
        // You may replace the UI if wanted
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    class Coordinator: NSObject, AVPlayerViewControllerDelegate {
        let guardian: AVVideoPlayer
        
        init(_ guardian: AVVideoPlayer) {
            self.guardian = guardian
        }
        
        func playerViewControllerWillStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
            guardian.viewModel.pipStatus = .willStart
        }
        
        func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
            guardian.viewModel.pipStatus = .didStart
        }
        
        func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
            guardian.viewModel.pipStatus = .willStop
        }
        
        func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
            guardian.viewModel.pipStatus = .didStop
        }
    }
}

That is the working code I obtained from Right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments