This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ViewController.swift
151 lines (123 loc) · 4.73 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// ViewController.swift
// uipip
//
// Created by Akihiro Urushihara on 2021/11/27.
//
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
private let _pipContent = VideoProvider()
private var _pipController: AVPictureInPictureController?
private let _bufferDisplayLayer = AVSampleBufferDisplayLayer()
private var _pipPossibleObservation: NSKeyValueObservation?
private var _observer: NSObjectProtocol?
private let _startButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
let session = AVAudioSession.sharedInstance()
try! session.setCategory(.playback, mode: .moviePlayback)
try! session.setActive(true)
let margin = ((view.bounds.width - 200) / 2)
_startButton.frame = .init(x: margin, y: 80, width: 200, height: 30)
_startButton.addTarget(self, action: #selector(ViewController.toggle), for: .touchUpInside)
_startButton.setTitle("Start PiP", for: .normal)
self.view.addSubview(_startButton)
// 表示用の画面を作成
let videoContainerView = UIView()
videoContainerView.frame = .init(x: margin, y: 200, width: 200, height: 30)
self.view.addSubview(videoContainerView)
let bufferDisplayLayer = _pipContent.bufferDisplayLayer
bufferDisplayLayer.frame = videoContainerView.bounds
bufferDisplayLayer.videoGravity = .resizeAspect
videoContainerView.layer.addSublayer(bufferDisplayLayer)
_pipContent.start()
DispatchQueue.main.async {
self.start()
}
}
func start() {
// PinP をサポートしているデバイスかどうかを確認
if AVPictureInPictureController.isPictureInPictureSupported() {
// AVPictureInPictureController の生成
_pipController = AVPictureInPictureController(
contentSource: .init(
sampleBufferDisplayLayer:
_pipContent.bufferDisplayLayer,
playbackDelegate: self))
_pipController?.delegate = self
_pipPossibleObservation = _pipController?.observe(
\AVPictureInPictureController.isPictureInPicturePossible,
options: [.initial, .new]) { [weak self] _, change in
guard let self = self else { return }
// 再生可能になったら PinP ボタンを有効化
if (change.newValue ?? false) {
self._startButton.isEnabled = (change.newValue ?? false)
}
}
}
}
@objc func toggle() {
guard let _pipController = _pipController else { return }
if !_pipController.isPictureInPictureActive {
_pipController.startPictureInPicture()
} else {
_pipController.stopPictureInPicture()
}
}
}
extension ViewController: AVPictureInPictureControllerDelegate {
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
failedToStartPictureInPictureWithError error: Error
) {
print("\(#function)")
print("pip error: \(error)")
}
func pictureInPictureControllerWillStartPictureInPicture(
_ pictureInPictureController: AVPictureInPictureController
) {
print("\(#function)")
}
func pictureInPictureControllerWillStopPictureInPicture(
_ pictureInPictureController: AVPictureInPictureController
) {
print("\(#function)")
}
}
extension ViewController: AVPictureInPictureSampleBufferPlaybackDelegate {
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
setPlaying playing: Bool
) {
print("\(#function)")
}
func pictureInPictureControllerTimeRangeForPlayback(
_ pictureInPictureController: AVPictureInPictureController
) -> CMTimeRange {
print("\(#function)")
return CMTimeRange(start: .negativeInfinity, duration: .positiveInfinity)
}
func pictureInPictureControllerIsPlaybackPaused(
_ pictureInPictureController: AVPictureInPictureController
) -> Bool {
print("\(#function)")
return false
}
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
didTransitionToRenderSize newRenderSize: CMVideoDimensions
) {
print("\(#function)")
print(newRenderSize)
}
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
skipByInterval skipInterval: CMTime,
completion completionHandler: @escaping () -> Void
) {
print("\(#function)")
completionHandler()
}
}