๐ŸŽ iOS/UIkit

AVPlayer, AVPlayItem

JerryiOS 2023. 6. 29. 18:31

AVPlayer

๋ฏธ๋””์–ด Player์˜ ์ „์†ก ๋™์ž‘์„ ์ œ์–ดํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด.
  • ์‰ฝ๊ฒŒ๋งํ•˜๋ฉด .. ์žฌ์ƒ ํ”Œ๋ ˆ์ด์–ด์˜ ๋™์ž‘์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ฐ์ฒด์ธ๋“ฏ
  • AVAsset์˜ ์ „๋ฐ˜์ ์ธ playback(๋…น์Œ, ๋…นํ™”, ์žฌ์ƒ)์„ ์ œ์–ดํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด

๊ตฌ์กฐ

https://ios-development.tistory.com/928

  • AVPlayer๋Š” ํ•œ ๋ฒˆ์— ํ•˜๋‚˜๋งŒ ์žฌ์ƒ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์—ฌ๋Ÿฌ ๋ฏธ๋””์–ด๋ฅผ ์žฌ์ƒํ•˜๊ณ  ์‹ถ์œผ๋ฉด AVQueuePlayer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉ

 

AVPlayerItem

๋ฏธ๋””์–ด ํ”Œ๋ ˆ์ด์–ด์— ๋Œ€ํ•œ ์‹œ๊ฐ„๊ณผ ์ƒํƒœ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด.
  • AVAsset: ์ •์ ์ธ ์ƒํƒœ๊ด€๋ฆฌ (์ด ์žฌ์ƒ์‹œ๊ฐ„, ์ƒ์„ฑ๋‚ ์งœ)
  • AVPlayerItem: ๋™์ ์ธ ์ƒํƒœ๊ด€๋ฆฌ (presentation state, ํ˜„์žฌ์‹œ๊ฐ„, ํ˜„์žฌ๊นŒ์ง€ ์žฌ์ƒ๋œ ์‹œ๊ฐ„ ๋“ฑ)

 

๊ตฌ์กฐ

  • AVPlayerItem ๋‚ด๋ถ€์˜ AVPlayerItemTrack ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด track๋“ค์„ ์กฐ์ •ํ•˜์—ฌ,
    ์˜์ƒ ๋ฐ์ดํ„ฐ์—์„œ video ํŠธ๋ž™๋งŒ ํ™œ์„ฑํ™”์‹œํ‚ค๊ณ  audio ํŠธ๋ž™์€ ๋น„ํ™œ์„ฑํ™” ์‹œํ‚ค๋Š” ์ž‘์—… ๊ฐ€๋Šฅ

AVPlayerItemTrack ์˜ˆ์‹œ

 

AVPlayer ์ƒํƒœ ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ• - ์˜ต์ €๋น™

AVPlayer์™€ AVPlayerItem์€ ์ƒํƒœ๊ฐ€ ์ง€์†์ ์œผ๋กœ ๋ณ€ํ•˜๋ฏ€๋กœ, ์ƒํƒœ์— ๋Œ€ํ•œ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด KVO๋ฅผ ํ†ตํ•ด ๊ตฌ๋…ํ•˜์—ฌ ์‚ฌ์šฉ
// VideoCropViewController.swift

extension AVPlayerItem {
    public class let timeJumpedNotification: NSNotification.Name
    public class let recommendedTimeOffsetFromLiveDidChangeNotification: NSNotification.Name // the value of recommendedTimeOffsetFromLive has changed
    public class let mediaSelectionDidChangeNotification: NSNotification.Name // a media selection group changed its selected option
    
    // notification userInfo key                                                                    type
    public class let timeJumpedOriginatingParticipantKey: String
    
    public enum Status : Int, @unchecked Sendable {    
        case unknown = 0
        case readyToPlay = 1
        case failed = 2
    }
}
extension NSNotification.Name {

    @available(iOS 4.0, *)
    public static let AVPlayerItemDidPlayToEndTime: NSNotification.Name

    @available(iOS 4.3, *)
    public static let AVPlayerItemFailedToPlayToEndTime: NSNotification.Name

    @available(iOS 6.0, *)
    public static let AVPlayerItemPlaybackStalled: NSNotification.Name

    @available(iOS 6.0, *)
    public static let AVPlayerItemNewAccessLogEntry: NSNotification.Name

    @available(iOS 6.0, *)
    public static let AVPlayerItemNewErrorLogEntry: NSNotification.Name
}

๋น„๋””์˜ค ์žฌ์ƒ ๋ฐฉ๋ฒ•

AVKit

  •  
import AVKit

// ๋น„๋””์˜ค ํŒŒ์ผ์˜ URL
let videoURL = URL(string: "https://example.com/video.mp4")!

// AVPlayerViewController ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
let playerViewController = AVPlayerViewController()

// AVPlayer ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
let player = AVPlayer(url: videoURL)

// AVPlayerViewController์— AVPlayer ํ• ๋‹น
playerViewController.player = player

// ๋น„๋””์˜ค ์žฌ์ƒ
present(playerViewController, animated: true) {
    player.play()
}

AVPlayerLayer

import AVFoundation
import AVKit
import UIKit

class ViewController: UIViewController {
    var player: AVPlayer?
    var playerLayer: AVPlayerLayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        // ๋น„๋””์˜ค ํŒŒ์ผ์˜ URL
        guard let videoURL = URL(string: "https://example.com/video.mp4") else {
            return
        }

        // AVPlayer ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
        player = AVPlayer(url: videoURL)

        // AVPlayerLayer ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
        playerLayer = AVPlayerLayer(player: player)
        playerLayer?.frame = view.bounds

        // AVPlayerLayer๋ฅผ ๋ทฐ์˜ layer์— ์ถ”๊ฐ€
        if let playerLayer = playerLayer {
            view.layer.addSublayer(playerLayer)
        }

        // ๋น„๋””์˜ค ์žฌ์ƒ
        player?.play()
    }
}

 

  • ๋ณดํ†ต ๋””์ž์ธ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— AVKit๋ณด๋‹ค๋Š” AVPlayerLayer๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • AVFoundation์—์„œ๋Š” Core Media ํ”„๋ ˆ์ž„์›Œํฌ์˜ CMTime ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋ถ€๋™์†Œ์ˆ˜์ ์˜ ๋ถ€์ •ํ™•์„ฑ์œผ๋กœ ์ธํ•ด ๋””ํ…Œ์ผํ•œ ์‹œ๊ฐ„์˜ ์ž‘์—…์ด ํ•„์š”ํ•œ ๋ฏธ๋””์–ด ์žฌ์ƒ์—์„œ์˜ ๋ฌธ์ œ์ ์„ ๋ง‰๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

Ref

https://ios-development.tistory.com/928

๋ฐ˜์‘ํ˜•