Skip to content

Latest commit

ย 

History

History
180 lines (151 loc) ยท 7.63 KB

Delivering_Media_Playback_AVKit.md

File metadata and controls

180 lines (151 loc) ยท 7.63 KB
layout title date categories tags author mathjax comments
post
Delivering Intuitive Media Playback with AVKit
2019-11-10 21:26:06 +0900
iOS
iOS WWDC Link WebKit
Juhee Kim
true
true

AVKit in the Stack

AVKit์€ CoreMedia์˜ AVFoundation์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌ์ถ• ๋œ ํฌ๋กœ์Šค ํ”Œ๋žซํผ ๋ฏธ๋””์–ด ์žฌ์ƒ UI framework์ž…๋‹ˆ๋‹ค. Apple์˜ ์ž์ฒด ์•ฑ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ AVPlayer ๊ธฐ๋ฐ˜ ๋ฏธ๋””์–ด ์ปจํ…์ธ ๋ฅผ ์‰ฝ๊ฒŒ ํ‘œ์‹œํ•˜๊ณ  ์žฌ์ƒํ•  ์ˆ˜ ์žˆ๋„๋กํ•˜๋Š” ๊ฒƒ์ด ์šฐ๋ฆฌ์˜ ์ž„๋ฌด์ž…๋‹ˆ๋‹ค.

  • UIKit ์•ฑ์˜ ๊ฒฝ์šฐ AVPlayerViewController๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • APKit ์•ฑ์˜ ๊ฒฝ์šฐ AVPlayerView๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ๋˜ํ•œ UIKit ๋ฐ APKit ๋ชจ๋‘ ์žฌ์ƒ ์„ ํƒํ™”๋ฉด์„ ์ถ”๊ฐ€ํ•˜๋Š” AVRoutePickerView๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
// Media Playback with AVPlayerViewController on iOS and tvOS
import AVKit

 // 1) AVPlayer ๋งŒ๋“ค๊ธฐ
 let player = AVPlayer(url: โ€œhttps://my.example/video.m3u8โ€)

// 2) AVPlayerViewController ๋งŒ๋“ค๊ณ  AVPlayer ๊ฐ์ฒด ํ• ๋‹นํ•ด์ฃผ๊ธฐ
let playerViewController = AVPlayerViewController()
playerViewController.player = player

 // 3) present AVPlayerViewController
 present(playerViewController, animated: true)

Media playback with AVKit

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Apple์ด ์ œ๊ณตํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ(๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ ์•ฑ๊ณผ ๋™์ผํ•œ) UI์˜ ๋ฏธ๋””์–ดํ”Œ๋ ˆ์ด์–ด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ CoreMedia ํ”Œ๋ ˆ์ด์–ด UI์•ˆ์—์„œ AVFoundation์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ - AVPlayer, AVPlayerItem, AVAsset - ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด์„œ๋„ AVKit์€ UIKit ๋ฐ AppKit ์œ„์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ Apple platform์— ๋Œ€์‘๋˜๋Š” ๊ณ ์œ  UI๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋‚˜์€ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

What's new for iOS 13

AVPlayerViewController

  • Full screen callbacks
  • AVPlayerViewController in iPad apps on the Mac
  • External metadata
  • Improved support for custom controls

Full screen callbacks

AVPlayerViewControllerDelegate : ์ „์ฒด ํ™”๋ฉด ์žฌ์ƒ์ด ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ๋๋‚  ๋•Œ ์•Œ๋ฆผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ, iOS 12 ๋ถ€ํ„ฐ ๊ฐ€๋Šฅ

@available(iOS 12.0, *)
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator)

func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
  coordinator.animate(alongsideTransition: { (context) in
    // Add coordinated animations
  }) { (context) in
    // ์—ฌ๊ธฐ์„œ ์ „ํ™˜์ด ์„ฑ๊ณตํ–‡๋Š”์ง€, ์ทจ์†Œ๋๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    if context.isCancelled {
      // Still embedded inline
    } else {
      // Presented full screen
      // Take strong reference to playerViewController if needed
    }
  }
}

๋งŒ์•ฝ ์Šคํฌ๋กค๋ทฐ์— ํ”Œ๋ ˆ์ด์–ด ๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ์ „์ฒดํ™”๋ฉด ์ƒํƒœ์—์„œ ๊ธฐ๊ธฐ๊ฐ€ ํšŒ์ „ํ•˜๋ฉด์„œ ์Šคํฌ๋กค๋ทฐ ์˜คํ”„์…‹์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ํ…Œ์ด๋ธ” ๋ทฐ๋‚˜ ์ปฌ๋ ‰์…˜ ๋ทฐ์—์„œ ์ด๋Ÿด ๊ฒฝ์œ  ๋ทฐ ์ž์ฒด๊ฐ€ ํ• ๋‹นํ•ด์ œ๋  ์ˆ˜ ์žˆ์–ด์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ context๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ „์ฒดํ™”๋ฉด์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ ๋‹ค์Œ ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ฐ•ํ•œ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

AVPlayerViewController in iPad Apps on the Mac

  • Exact same API as on iOS Picture-in-picture support
  • Includes AVPictureInPictureController (also for AppKit) And AVPlayerView, for AppKit-based apps
  • Touch Bar, keyboard, and Now Playing support Audio and AirPlay routing
  • Available in macOS 10.15

External Metadata

Title, artwork, ํ˜น์€ ๊ทธ ์™ธ์˜ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ AVKit์ด ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ํ˜น์€ ์›ํ•˜๋Š” Metadata๋ฅผ ๋ณด์™„ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@available(iOS 12.0, *)
extension AVPlayerItem {
  open var externalMetadata: [AVMetadataItem] }
}

Custom Playback Controls

  • showsPlaybackControls = false
  • present AVPlayerViewController modally
  • Add controls to contentOverlayView
  • For the best user experience, you should:
    • Override UIViewController methods for status bar, home indicator
    • Pass unhandled touches through your view
    • Let AVKit handle double-tap for video zoom

AVPlayerViewController on iOS : Best practices

  • Showing full screen
    • Covers UIWindowScene coordinate space
      • Splash screen
      • Full screen playback
  • Embedding inline
  • Picture-in-picture

Showing Full Screen Usecase1: Splash screen

// Add as child:
parent.addChild(playerViewController)
parent.view.addSubview(playerViewController.view) // Or other UIView insertion API

// Enable auto layout and set up constraints
playerViewController.didMove(toParent: parent)

// Remove from parent:
playerViewController.willMove(toParent: nil)
playerViewController.view.removeFromSuperview()
playerViewController.removeFromParent()

// ์žฌ์ƒ ์ปจํŠธ๋กค์„ ํ‘œ์‹œํ•˜์ง€ ์•Š์Œ:
playerViewController.showsPlaybackControls = false

// ์ „์ฒด ํ™”๋ฉด์„ ์ฑ„์šฐ๋„๋ก ํ•จ:
playerViewController.videoGravity = .resizeAspectFill

// ํ•„์š”ํ•˜๋‹ค๋ฉด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ปฌ๋Ÿฌ ๋ณ€๊ฒฝ:
playerViewController.view.backgroundColor = .clear // Or any other UIColor
  • AVPlayer.allowsExternalPlayback ๋น„ํ™œ์„ฑํ™”
  • AVAudioSession์„ 2์ฐจ media playback์œผ๋กœ ์„ค์ •
    • Use .ambient category
  • Observe AVAudioSession.silenceSecondaryAudioHintNotification
    • Honor AVAudioSession.secondaryAudioShouldBeSilencedHint

Showing Full Screen Usercase 2: Full screen playback

// Media Playback with AVPlayerViewController on iOS and tvOS
import AVKit
// 1a) Create an AVPlayer
let player = AVPlayer(url: โ€œhttps://my.example/video.m3u8โ€)
// 1b) Add external metadata if needed
player.currentItem?.externalMetadata = // Array of [AVMetadataItem]
// 2) Create an AVPlayerViewController
let playerViewController = AVPlayerViewController()
playerViewController.player = player
// 3) Show it
present(playerViewController, animated: true)
  • Present modally : ์ƒํƒœ ํ‘œ์‹œ ์ค„ ์ฒ˜๋ฆฌ๊ฐ€ ์šฉ์ด
  • Use the default modalPresentationStyle
    • ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ํ™”๋ฉด ๋ทฐ๊ฐ€ UIKit์— ์˜ํ•ด์„œ ์ œ๊ฑฐ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๏ฟฝ๋ฌธ
    • ํ™”๋ฉด์ด ํšŒ์ „๋  ๋•Œ ๋ณด์ด์ง€ ์•Š๋Š” ๋‹ค๋ฉด ๋ ˆ์ด์•„์›ƒ์„ ํ•  ์ด์œ ๊ฐ€ ์—†์Œ
  • Do not set AVPlayerViewController.videoGravity
  • ์ „์ฒด ํ™”๋ฉด ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” AVPlayerViewControllerDelegate๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค.
    • Do not rely on viewWillAppear(_:) and friends
  • AVPlayerItem๋ฅผ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด๋‘๊ธฐ
    • Set AVPlayer.rate for setting item
  • AVPlayer.status ์™€ AVPlayerItem.status ๊ด€์ฐฐํ•ด์„œ ์ œ์–ดํ•˜๊ธฐ
    • Donโ€™t begin playback until status is .readyToPlay
    • Check error property when status is .failed
      • Rebuild AVFoundation objects if .mediaServicesWereReset
  • Enable AVPlayer.usesExternalPlaybackWhileExternalScreenIsActive
  • Configure AVAudioSession for .playback

Embedding AVPlayerViewController Inline

  • Be prepared to begin and end full screen playback
    • AVPlayerViewControllerDelegate ์‚ฌ์šฉํ•ด์„œ ํŠธ๋ž˜ํ‚น
    • Keep strong reference to embedded AVPlayerViewController when full screen
    • Retarget dismissal animations when ending full screen presentation
  • Leave modalPresentationStyle as .fullScreen
  • Can style embedded content without impacting full screen:
    • videoGravity
    • view.layer.cornerRadius, cornerCurve, and maskedCorners
    • view.backgroundColor
  • For automatically entering and exiting full screen:
    • entersFullScreenWhenPlaybackBegins
    • exitsFullScreenWhenPlaybackEnds
  • Adopt UIViewController containment API