iOS Development

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

Spread the love


I did some learning on Image in Image for AVPlayerViewController. I am making an attempt 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 resolution for that. Is there any method or any work round I can obtain that?

last class AVPlayerViewModel: ObservableObject {

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

    let participant = AVPlayer()
    personal 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 {
           strive 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 possibly can 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 bought from Right here.

Leave a Reply

Your email address will not be published. Required fields are marked *