Skip to content

Commit f2dc731

Browse files
committed
動いていた commit に戻す
commit: 1b2811c
1 parent 7ff85ca commit f2dc731

File tree

1 file changed

+70
-24
lines changed

1 file changed

+70
-24
lines changed

Sora/SwiftUIVideoView.swift

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ import UIKit
77
*/
88
@available(iOS 14, *)
99
public struct SwiftUIVideoView<Background>: View where Background: View {
10-
@ObservedObject private var controller: VideoController
10+
private var stream: MediaStream?
1111
private var background: Background
12+
13+
// TODO(zztkm): わかりやすいコメントを書く
14+
// 親 View で定義された stopVideo 変数と接続するための変数
15+
@Binding private var isStop: Bool
16+
@Binding private var isClear: Bool
17+
18+
@ObservedObject private var controller: VideoController
19+
1220
/**
1321
ビューを初期化します。
1422

15-
- parameter controller: VideoController
23+
- parameter stream: 描画される映像ストリーム。 nil の場合は何も描画されません
24+
- parameter isStop: 映像の描画を制御するフラグ (default: false)
25+
- parameter isClear: 映像をクリアして背景 View を表示するためのフラグ (default: false)
1626
*/
17-
public init(_ controller: VideoController) where Background == EmptyView {
18-
self.init(controller, background: EmptyView())
27+
public init(_ stream: MediaStream?, isStop: Binding<Bool>? = nil, isClear: Binding<Bool>? = nil) where Background == EmptyView {
28+
self.init(stream, background: EmptyView(), isStop: isStop, isClear: isClear)
1929
}
2030

2131
/**
@@ -26,23 +36,22 @@ public struct SwiftUIVideoView<Background>: View where Background: View {
2636
- parameter isStop: 映像の描画を制御するフラグ (default: false)
2737
- parameter isClear: 映像をクリアして背景 View を表示するためのフラグ (default: false)
2838
*/
29-
public init(_ controller: VideoController, background: Background) {
30-
self.controller = controller
39+
public init(_ stream: MediaStream?, background: Background, isStop: Binding<Bool>? = nil, isClear: Binding<Bool>? = nil) {
40+
self.stream = stream
3141
self.background = background
42+
// 指定がない場合は固定値 false を与える
43+
_isStop = isStop ?? .constant(false)
44+
_isClear = isClear ?? .constant(false)
45+
controller = VideoController(stream: stream)
3246
}
3347

3448
/// :nodoc:
35-
/// TODO(zztkm): SwiftUI での Background と UIKit での Background が同居しているが使い分けについて考える
36-
/// 今の実装だと isClear で UIKit の方も clear してるけど、これは意味がなくて isClear すると SwiftUI の Backgroud View が表示されるようになる
37-
/// なので、そもそも VideView の clear() method を呼ぶ意味がなくなってしまってる
38-
/// この辺の制御はできるだけ SwiftUI 側に寄せたいので、clear() metthod を呼ばないようにするなど検討したい
3949
public var body: some View {
4050
ZStack {
41-
// isClear が true のときは背景を表示し、false のときは Video を表示する
4251
background
43-
.opacity(isClear ? 1 : 0)
52+
.opacity(controller.isCleared ? 1 : 0)
4453
RepresentedVideoView(controller, isStop: $isStop, isClear: $isClear)
45-
.opacity(isClear ? 0 : 1)
54+
.opacity(controller.isCleared ? 0 : 1)
4655
}
4756
}
4857

@@ -76,7 +85,33 @@ public struct SwiftUIVideoView<Background>: View where Background: View {
7685

7786
/// 映像のクリア時に表示する背景ビューを指定します。
7887
public func videoBackground<Background>(_ background: Background) -> SwiftUIVideoView<Background> where Background: View {
79-
SwiftUIVideoView<Background>(controller, background: background)
88+
var new = SwiftUIVideoView<Background>(stream, background: background)
89+
new.controller = controller
90+
return new
91+
}
92+
93+
/**
94+
映像の描画を停止します。
95+
*/
96+
private func videoStop(_ flag: Bool) -> SwiftUIVideoView<Background> {
97+
if flag {
98+
controller.videoView.stop()
99+
} else if !controller.videoView.isRendering {
100+
controller.videoView.start()
101+
}
102+
return self
103+
}
104+
105+
/**
106+
画面を背景ビューに切り替えます。
107+
このメソッドは描画停止時のみ有効です。
108+
*/
109+
public func videoClear(_ flag: Bool) -> SwiftUIVideoView<Background> {
110+
if flag {
111+
controller.videoView.clear()
112+
controller.isCleared = true
113+
}
114+
return self
80115
}
81116

82117
/// 映像のサイズの変更時に実行されるブロックを指定します。
@@ -90,6 +125,24 @@ public struct SwiftUIVideoView<Background>: View where Background: View {
90125
controller.videoView.handlers.onRender = perform
91126
return self
92127
}
128+
129+
/// 映像フレームの描画開始時に実行されるブロックを指定します。
130+
public func videoOnStart(perform: @escaping () -> Void) -> SwiftUIVideoView<Background> {
131+
controller.videoView.handlers.onStart = perform
132+
return self
133+
}
134+
135+
/// 映像フレームの描画停止時に実行されるブロックを指定します。
136+
public func videoOnStop(perform: @escaping () -> Void) -> SwiftUIVideoView<Background> {
137+
controller.videoView.handlers.onStop = perform
138+
return self
139+
}
140+
141+
/// 映像が backgroundView に切り替わったときに実行されるブロックを指定します。
142+
public func videoOnClear(perform: @escaping () -> Void) -> SwiftUIVideoView<Background> {
143+
controller.videoView.handlers.onClear = perform
144+
return self
145+
}
93146
}
94147

95148
/*
@@ -99,11 +152,7 @@ private struct RepresentedVideoView: UIViewRepresentable {
99152
typealias UIViewType = VideoView
100153

101154
@ObservedObject private var controller: VideoController
102-
103-
// 以下は SDK ユーザー側で制御される変数
104-
/// 映像を停止するかを制御するフラグ
105155
@Binding private var isStop: Bool
106-
/// backgroundView を表示するか制御するフラグ
107156
@Binding private var isClear: Bool
108157

109158
public init(_ controller: VideoController, isStop: Binding<Bool>, isClear: Binding<Bool>) {
@@ -127,11 +176,6 @@ private struct RepresentedVideoView: UIViewRepresentable {
127176
controller.stream?.videoRenderer = uiView
128177
}
129178

130-
// 現在 VideoView が表示している映像の元々のフレームサイズを返します。
131-
public var currentVideoFrameSize: CGSize? {
132-
controller.videoView.currentVideoFrameSize
133-
}
134-
135179
/**
136180
映像の描画を停止します。TODO(zztkm): method 名の検討 (stop start の toggle なので、stop はおかしいかも?
137181
*/
@@ -156,13 +200,15 @@ private struct RepresentedVideoView: UIViewRepresentable {
156200
}
157201
}
158202

159-
public class VideoController: ObservableObject {
203+
class VideoController: ObservableObject {
160204
var stream: MediaStream?
161205

162206
// init() で VideoView を生成すると次のエラーが出るので、生成のタイミングを遅らせておく
163207
// Failed to bind EAGLDrawable: <CAEAGLLayer: 0x********> to GL_RENDERBUFFER 1
164208
lazy var videoView = VideoView()
165209

210+
@Published var isCleared: Bool = false
211+
166212
init(stream: MediaStream?) {
167213
self.stream = stream
168214
}

0 commit comments

Comments
 (0)