[swift] 팝업창에서 비디오 플레이어를 사용하는 방법 - Swift PopupDialog 사용법

이번 포스트에서는 Swift 언어를 사용하여 팝업창에서 비디오 플레이어를 구현하는 방법을 알아보겠습니다. 팝업창을 생성하기 위해 PopupDialog 라이브러리를 사용할 것이며, 비디오 플레이어를 위해 AVPlayerViewController를 활용할 것입니다.

PopupDialog 설치하기

먼저 PopupDialog 라이브러리를 설치해야 합니다. CocoaPods를 사용한다면 프로젝트의 Podfile에 다음을 추가해주세요.

pod 'PopupDialog'

설치가 완료되면 해당 프로젝트의 폴더에서 터미널을 열고 pod install 명령어를 실행합니다.

팝업창과 비디오 플레이어 구현하기

1. 팝업창 생성하기

먼저 팝업창을 생성합니다. ViewController의 적절한 위치에서 팝업창을 띄우고자 하는 이벤트를 처리합니다. 아래는 팝업창을 생성하는 예시 코드입니다.

import PopupDialog

// 팝업창 생성 이벤트 처리
@IBAction func showPopupDialog(_ sender: UIButton) {
    // 팝업창 내용 구성
    let popupTitle = "비디오 플레이어"
    let message = "비디오를 재생할 준비가 되었습니다."
    
    // 팝업창 생성
    let popup = PopupDialog(title: popupTitle, message: message)
    
    // 팝업창 닫기 버튼 추가
    let button = DefaultButton(title: "닫기") {
        // 팝업창 닫기
        popup.dismiss(animated: true, completion: nil)
    }
    popup.addButton(button)
    
    // 팝업창 표시
    self.present(popup, animated: true, completion: nil)
}

2. 비디오 플레이어 추가하기

다음으로 비디오 플레이어를 팝업창에 추가해야 합니다. 이를 위해 AVPlayerViewController를 활용할 것입니다. 아래는 비디오 플레이어를 팝업창에 추가하는 예시 코드입니다.

import PopupDialog
import AVKit

// 팝업창 생성 이벤트 처리
@IBAction func showPopupDialog(_ sender: UIButton) {
    // ...

    // AVPlayerViewController 인스턴스 생성
    let playerViewController = AVPlayerViewController()
    playerViewController.player = AVPlayer(url: URL(string: "비디오 파일 URL")!)
    
    // 비디오 플레이어를 팝업창에 추가
    popup.addVC(playerViewController)
    
    // ...
}

참고 자료

이제 PopupDialog를 사용하여 팝업창에서 비디오 플레이어를 구현하는 방법에 대해 알아보았습니다. 이를 참고하여 프로젝트에 적용해 보세요.