-1

I'm trying to implement a picture in picture custom player, with the following setup up:

private func setupCustomPlayer(){
    let playerLayer = AVPlayerLayer(player: player)
        
    playerLayer.frame = videoView.bounds
    playerLayer.videoGravity = .resizeAspectFill
    videoView.layer.addSublayer(playerLayer)
    
    pip = AVPictureInPictureController(playerLayer: playerLayer)
    pip.canStartPictureInPictureAutomaticallyFromInline = true
    pip.delegate = self
    
    player?.play()
}

However, the picture in picture does not working, also I set up the audio session in app delegate:

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playback, mode: .moviePlayback)
    } catch {
        print("Setting category to AVAudioSessionCategoryPlayback failed.")
    }

The question is. what could be happening. I'm using this source code to guide me: https://github.com/sharmavipin11289/PIP, but I don't have lucky.

There's my UIViewControllerRepresentable class:

func makeUIViewController(context: Context) -> some PlayerViewController {
    let vc = PlayerViewController(url: url, showsHelp: false) {
        fullscreen.toggle()
        if fullscreen {
            self.changeOrientation(to: .landscapeLeft)
        } else {
            self.changeOrientation(to: .portrait)
        }
    }
    return vc
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    
}

Can anybody help me?

Best Regards

Alfredo Luco G
  • 876
  • 7
  • 18
  • You also need to enable Picture in Picture in background mode check this link: [link](https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/configuring_the_audio_playback_of_ios_and_tvos_apps) . Also try setting `.longFormAudio` instead of `moviePlayback` – Mr.SwiftOak Sep 19 '22 at 12:17
  • hi @Mr.SwiftOak I tried that but nothing works, but works in a simple AVPlayerController. What could be happening? – Alfredo Luco G Oct 24 '22 at 06:32

1 Answers1

0

The issue can be solved upgrading from iOS 14 to iOS 16 and using the following audio session in App Delegate:

let audioSession = AVAudioSession.sharedInstance()
 do {
     try audioSession.setCategory(.playback)
     try audioSession.setActive(true, options: [])
 } catch {
     print("Setting category to AVAudioSessionCategoryPlayback failed.")
 }
Alfredo Luco G
  • 876
  • 7
  • 18