[swift] 동영상 반복 재생 기능 구현하기

iOS 앱을 개발하다보면 동영상을 반복해서 재생하는 기능이 필요한 경우가 있습니다. Swift를 사용하여 동영상을 반복해서 재생하는 간단한 방법에 대해 알아보겠습니다.

AVPlayerLooper 사용하기

AVPlayerLooper를 사용하면 AVPlayer를 사용하여 동영상을 반복 재생할 수 있습니다. 아래는 AVPlayerLooper를 사용하여 동영상을 반복 재생하는 방법입니다.

import AVKit

if let path = Bundle.main.path(forResource: "example", ofType: "mp4") {
    let videoURL = URL(fileURLWithPath: path)
    let player = AVQueuePlayer()
    let playerItem = AVPlayerItem(url: videoURL)
    let looper = AVPlayerLooper(player: player, templateItem: playerItem)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    present(playerViewController, animated: true) {
        player.play()
    }
}

위의 예제 코드에서 “example.mp4”는 프로젝트 내에 포함된 동영상 파일의 이름입니다. AVPlayerLooper를 사용하여 AVPlayer를 초기화하고, AVPlayerViewController를 통해 동영상을 재생합니다.

결론

Swift를 사용하여 iOS 앱에서 동영상을 반복 재생하는 방법에 대해 알아보았습니다. AVPlayerLooper를 사용하면 간단하게 동영상을 반복 재생할 수 있습니다. 동영상 반복 재생 기능을 적용하여 사용자에게 더 나은 경험을 제공해보세요!

더 자세한 내용은 AVFoundation 문서를 참고하시기 바랍니다.