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
์ 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)
์ด๋ ๊ฒ ํ๋ฉด Apple์ด ์ ๊ณตํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ(๊ทธ๋ฆฌ๊ณ ๊ธฐ๋ณธ ์ฑ๊ณผ ๋์ผํ) UI์ ๋ฏธ๋์ดํ๋ ์ด์ด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ๋ํ CoreMedia
ํ๋ ์ด์ด UI์์์ AVFoundation
์ ๋ชจ๋ ๊ธฐ๋ฅ - AVPlayer
, AVPlayerItem
, AVAsset
- ๋ค์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ฉด์๋ AVKit์ UIKit ๋ฐ AppKit ์์ ์๊ธฐ ๋๋ฌธ์ ๊ฐ Apple platform์ ๋์๋๋ ๊ณ ์ UI๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ๋ ๋์ ๊ฒฝํ์ ์ ๊ณตํ ์ ์์ต๋๋ค.
- Full screen callbacks
- AVPlayerViewController in iPad apps on the Mac
- External metadata
- Improved support for custom controls
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๊ฐ ์ ์์ ์ผ๋ก ์ ์ฒดํ๋ฉด์ผ๋ก ๋ณ๊ฒฝ๋์๋์ง ํ์ธํ ๋ค์ ํ์ํ๋ค๋ฉด ๊ฐํ ์ฐธ์กฐ
๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- 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
Title, artwork, ํน์ ๊ทธ ์ธ์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ AVKit์ด ์๋์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค.
ํน์ ์ํ๋ Metadata๋ฅผ ๋ณด์ํ๋๋ก ํ ์ ์์ต๋๋ค.
@available(iOS 12.0, *)
extension AVPlayerItem {
open var externalMetadata: [AVMetadataItem] }
}
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
- Showing full screen
- Covers
UIWindowScene
coordinate space- Splash screen
- Full screen playback
- Covers
- Embedding inline
- Picture-in-picture
// 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
- Use
- Observe
AVAudioSession.silenceSecondaryAudioHintNotification
- Honor
AVAudioSession.secondaryAudioShouldBeSilencedHint
- Honor
// 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
- Rebuild AVFoundation objects if
- Donโt begin playback until status is
- Enable
AVPlayer.usesExternalPlaybackWhileExternalScreenIsActive
- Configure
AVAudioSession
for.playback
- 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