[swift] AVPlayer를 사용한 Swift 비디오 재생

iOS 앱을 개발하다 보면 비디오를 재생해야 하는 경우가 많이 있습니다. AVPlayer는 Apple의 AVFoundation 프레임워크에서 제공하는 클래스로, Swift에서 비디오를 재생하는 데 유용하게 활용됩니다.

AVPlayer란?

AVPlayer는 미디어 콘텐츠(예: 비디오 파일)를 재생하기 위한 객체로, AVFoundation 프레임워크에서 제공합니다. 이를 사용하여 비디오를 로드하고 재생할 수 있습니다.

AVPlayer를 사용한 비디오 재생하기

AVPlayer를 사용하여 Swift에서 비디오를 재생하는 방법은 간단합니다. 먼저 AVKit 프레임워크를 import하고 AVPlayerViewController를 생성한 다음, AVPlayer 객체를 생성하여 비디오를 로드하고 AVPlayerViewController에 할당합니다.

import AVKit
import AVFoundation

if let videoURL = URL(string: "your_video_url_here") {
    let player = AVPlayer(url: videoURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    present(playerViewController, animated: true) {
        player.play()
    }
}

위 코드에서 “your_video_url_here” 부분에는 재생하려는 비디오 파일의 URL을 넣어주어야 합니다.

결론

AVPlayer를 사용하여 Swift에서 비디오를 재생하는 방법을 알아보았습니다. AVPlayer를 사용하면 간단한 코드 몇 줄로 비디오를 로드하고 재생할 수 있습니다. iOS 앱을 개발할 때 비디오 재생 기능을 구현해야 할 때는 AVPlayer를 활용하여 쉽게 구현할 수 있습니다.

많은 경우 AVPlayer를 customizing하여 비디오 재생 화면의 UI를 사용자 정의하는 등의 작업이 필요할 수 있습니다. 자세한 내용은 Apple의 공식 문서를 참고하시기 바랍니다.