[WWDC21] What’s new in AVKit

WWDC21 영상보면서 이해한대로 대충 끄적여보기 첼린지

정확한 이해가 필요하신 분들은 영상을 직접 시청하는 것을 권합니다.

이게 PiP 관련해서 새로운 controller가 나온건데, 기존에 있던 AVPlayerViewController로도 그냥 써도 상관없다고 하네요..

영상 보시면 아실텐데 메시지나 push 알림이 왔을 때, 그 알림을 누르면 그 앱으로 이동하고, 보던 영상은 PiP 모드로 자동 전환됩니다. 근데 이게 기존에 있던 AVPlayerViewController로도 가능하다고 하니까 더 해줘야할 건 없다고 하네요??

1분 16초 - New canStartPictureInPictureAutomaticallyFromInline property

// New property on AVPlayerViewController / AVPictureInPictureController.
var canStartPictureInPictureAutomaticallyFromInline: Bool { get set }

이번에 추가되는 controller에도 있는데요.. 아 근데 이게 정확히 AVPlayerViewController의 기능도 그대로 하면서 되는 그런거라면 매우 좋을텐테 직접 써봐야 알 것 같습니다.

property 이름이 정말 긴데 그냥 canPiPAutoFromInline 이렇게 했으면 안됐나.. 희망사항입니다.

1분 40초 - Setting up AVPictureInPictureController with an AVPlayLayer

func setupPictureInPicture() {
    // Ensure PiP is supported by current device.
    if AVPictureInPictureController.isPictureInPictureSupported() {
        // Create a new controller, passing the reference to the AVPlayerLayer.
        pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
        pictureInPictureController.delegate = self
        
        // Observe AVPictureInPictureController.isPictureInPicturePossible to update the PiP
        // button’s enabled state.
    } else {
        // PiP isn't supported by the current device. Disable the PiP button.
        pictureInPictureButton.isEnabled = false
    }
}

이렇게 생성할 수 있나봐요.. 저 isPicturInPictureSupported는 static function인 것 같아요

이걸 먼저 체크하고 인스턴스를 생성하는거보니 음.. 먼저 디바이스가 PiP 모드를 지원하는지 체크를 하고 생성해야하나봐요.. 지원 안하는 것도 있나.. 뭐.. 조심해서 나쁠 건 없으니까요 ㅎㅎ

2분 11초 - Starting and stopping picture in picture

@IBAction func togglePictureInPictureMode(_ sender: UIButton) {
    if pictureInPictureController.isPictureInPictureActive {
        pictureInPictureController.stopPictureInPicture()
    } else {
        pictureInPictureController.startPictureInPicture()
    }
}

stop, start로 PiP 모드를 제어할 수 있다면, 그냥 버튼 이벤트 하나로 별다른 설정 없이 제어 가능하다는 건데, 이러면 재생은 AVPlayerViewController가하고 PiP 전환만 저게 하는건 뭔가 이상하지 않나요??

뇌피셜이긴한데 제발 이거 하나로 재생까지 다 했으면 좋겠네요..

2분 39초 - Setting up AVPictureInPictureController with an AVSampleBufferDisplayLayer

func setupPictureInPicture() {
    // Ensure PiP is supported by current device.
    if AVPictureInPictureController.isPictureInPictureSupported() {
        // Create a new controller, passing a reference to the content source.
				let source = AVPictureInPictureController.ContentSource(
																sampleBufferDisplayLayer: displayLayer,
																				playbackDelegate: self)
				pictureInPictureController = AVPictureInPictureController(contentSource: source)
				pictureInPictureController.delegate = self
        
        // Observe AVPictureInPictureController.isPictureInPicturePossible to update the PiP
        // button’s enabled state.
    } else {
        // PiP isn't supported by the current device. Disable the PiP button.
        pictureInPictureButton.isEnabled = false
    }
}

아.. 이건 왜 코드 제공안하는 건가요.. 직접 작성했습니다. AVSampleBufferDisplayLayer로도 생성할 수 있는 예시입니다. 근데 이게 아까도 말했듯이 PiP 모드만 제어하는건지 그걸 모르겠습니다..

나와서 써봐야할듯? 여기서 설정해주는 playbackDelegate는 PiP 모드에서 15초 전, 15초 후, 멈춤, 재생 등 Player에서 할 수 있는 대부분의 기능을 제어할 수 있는 delegate 입니다.

자세한 내용은

avpictureinpicturesamplebufferplaybackdelegate

문서를 참고해주세요~