@@ -14,6 +14,10 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
14
14
override func viewDidLoad( ) {
15
15
super. viewDidLoad ( )
16
16
// Do any additional setup after loading the view, typically from a nib.
17
+
18
+ session. delegate = self
19
+ session. preView = self . view
20
+
17
21
self . requestAccessForVideo ( )
18
22
self . requestAccessForAudio ( )
19
23
self . view. backgroundColor = UIColor . clear
@@ -23,6 +27,10 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
23
27
containerView. addSubview ( beautyButton)
24
28
containerView. addSubview ( cameraButton)
25
29
containerView. addSubview ( startLiveButton)
30
+
31
+ cameraButton. addTarget ( self , action: #selector( didTappedCameraButton ( _: ) ) , for: . touchUpInside)
32
+ beautyButton. addTarget ( self , action: #selector( didTappedBeautyButton ( _: ) ) , for: . touchUpInside)
33
+ startLiveButton. addTarget ( self , action: #selector( didTappedStartLiveButton ( _: ) ) , for: . touchUpInside)
26
34
}
27
35
28
36
override func didReceiveMemoryWarning( ) {
@@ -33,44 +41,45 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
33
41
//MARK: AccessAuth
34
42
35
43
func requestAccessForVideo( ) -> Void {
36
- let status = AVCaptureDevice . authorizationStatusForMediaType ( AVMediaTypeVideo)
44
+ let status = AVCaptureDevice . authorizationStatus ( forMediaType : AVMediaTypeVideo) ;
37
45
switch status {
38
46
// 许可对话没有出现,发起授权许可
39
- case AVAuthorizationStatus . NotDetermined :
40
- AVCaptureDevice . requestAccessForMediaType ( AVMediaTypeVideo, completionHandler: { ( granted) in
41
- if ( granted) {
42
- dispatch_async ( dispatch_get_main_queue ( ) , {
43
- self . session. running = true ;
44
- } ) ;
47
+ case AVAuthorizationStatus . notDetermined :
48
+ AVCaptureDevice . requestAccess ( forMediaType : AVMediaTypeVideo, completionHandler: { ( granted) in
49
+ if ( granted) {
50
+ DispatchQueue . main . async {
51
+ self . session. running = true
52
+ }
45
53
}
46
54
} )
47
55
break ;
48
56
// 已经开启授权,可继续
49
- case AVAuthorizationStatus . Authorized :
57
+ case AVAuthorizationStatus . authorized :
50
58
session. running = true ;
51
59
break ;
52
60
// 用户明确地拒绝授权,或者相机设备无法访问
53
- case AVAuthorizationStatus . Denied : break
54
- case AVAuthorizationStatus . Restricted : break ;
61
+ case AVAuthorizationStatus . denied : break
62
+ case AVAuthorizationStatus . restricted : break ;
55
63
default :
56
64
break ;
57
65
}
58
66
}
59
67
60
68
func requestAccessForAudio( ) -> Void {
61
- let status = AVCaptureDevice . authorizationStatusForMediaType ( AVMediaTypeAudio)
69
+ let status = AVCaptureDevice . authorizationStatus ( forMediaType : AVMediaTypeAudio)
62
70
switch status {
63
71
// 许可对话没有出现,发起授权许可
64
- case AVAuthorizationStatus . NotDetermined:
65
- AVCaptureDevice . requestAccessForMediaType ( AVMediaTypeAudio, completionHandler: { ( granted) in
72
+ case AVAuthorizationStatus . notDetermined:
73
+ AVCaptureDevice . requestAccess ( forMediaType: AVMediaTypeAudio, completionHandler: { ( granted) in
74
+
66
75
} )
67
76
break ;
68
77
// 已经开启授权,可继续
69
- case AVAuthorizationStatus . Authorized :
78
+ case AVAuthorizationStatus . authorized :
70
79
break ;
71
80
// 用户明确地拒绝授权,或者相机设备无法访问
72
- case AVAuthorizationStatus . Denied : break
73
- case AVAuthorizationStatus . Restricted : break ;
81
+ case AVAuthorizationStatus . denied : break
82
+ case AVAuthorizationStatus . restricted : break ;
74
83
default :
75
84
break ;
76
85
}
@@ -90,21 +99,23 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
90
99
func liveSession( _ session: LFLiveSession ? , liveStateDidChange state: LFLiveState ) {
91
100
print ( " liveStateDidChange: \( state. rawValue) " )
92
101
switch state {
93
- case LFLiveState . Ready :
102
+ case LFLiveState . ready :
94
103
stateLabel. text = " 未连接 "
95
104
break ;
96
- case LFLiveState . Pending :
105
+ case LFLiveState . pending :
97
106
stateLabel. text = " 连接中 "
98
107
break ;
99
- case LFLiveState . Start :
108
+ case LFLiveState . start :
100
109
stateLabel. text = " 已连接 "
101
110
break ;
102
- case LFLiveState . Error :
111
+ case LFLiveState . error :
103
112
stateLabel. text = " 连接错误 "
104
113
break ;
105
- case LFLiveState . Stop :
114
+ case LFLiveState . stop :
106
115
stateLabel. text = " 未连接 "
107
116
break ;
117
+ default :
118
+ break ;
108
119
}
109
120
}
110
121
@@ -127,13 +138,13 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
127
138
// 美颜
128
139
func didTappedBeautyButton( _ button: UIButton ) -> Void {
129
140
session. beautyFace = !session. beautyFace;
130
- beautyButton. selected = !session. beautyFace;
141
+ beautyButton. isSelected = !session. beautyFace
131
142
}
132
143
133
144
// 摄像头
134
145
func didTappedCameraButton( _ button: UIButton ) -> Void {
135
146
let devicePositon = session. captureDevicePosition;
136
- session. captureDevicePosition = ( devicePositon == AVCaptureDevicePosition . Back ) ? AVCaptureDevicePosition . Front : AVCaptureDevicePosition . Back ;
147
+ session. captureDevicePosition = ( devicePositon == AVCaptureDevicePosition . back ) ? AVCaptureDevicePosition . front : AVCaptureDevicePosition . back ;
137
148
}
138
149
139
150
// 关闭
@@ -144,26 +155,23 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
144
155
//MARK: - Getters and Setters
145
156
146
157
// 默认分辨率368 * 640 音频:44.1 iphone6以上48 双声道 方向竖屏
147
- lazy var session : LFLiveSession = {
148
- let audioConfiguration = LFLiveAudioConfiguration . defaultConfiguration ( )
149
- let videoConfiguration = LFLiveVideoConfiguration . defaultConfigurationForQuality ( LFLiveVideoQuality . Low3 )
158
+ var session : LFLiveSession = {
159
+ let audioConfiguration = LFLiveAudioConfiguration . defaultConfiguration ( for : LFLiveAudioQuality . high )
160
+ let videoConfiguration = LFLiveVideoConfiguration . defaultConfiguration ( for : LFLiveVideoQuality . low3 )
150
161
let session = LFLiveSession ( audioConfiguration: audioConfiguration, videoConfiguration: videoConfiguration)
151
-
152
- session? . delegate = self
153
- session? . preView = self . view
154
162
return session!
155
163
} ( )
156
164
157
165
// 视图
158
- lazy var containerView : UIView = {
159
- let containerView = UIView ( frame: CGRect ( x: 0 , y: 0 , width: self . view . bounds. size . width, height: self . view . bounds. size . height) )
166
+ var containerView : UIView = {
167
+ let containerView = UIView ( frame: CGRect ( x: 0 , y: 0 , width: UIScreen . main . bounds. width, height: UIScreen . main . bounds. height) )
160
168
containerView. backgroundColor = UIColor . clear
161
169
containerView. autoresizingMask = [ UIViewAutoresizing . flexibleHeight, UIViewAutoresizing . flexibleHeight]
162
170
return containerView
163
171
} ( )
164
172
165
173
// 状态Label
166
- lazy var stateLabel : UILabel = {
174
+ var stateLabel : UILabel = {
167
175
let stateLabel = UILabel ( frame: CGRect ( x: 20 , y: 20 , width: 80 , height: 40 ) )
168
176
stateLabel. text = " 未连接 "
169
177
stateLabel. textColor = UIColor . white
@@ -172,49 +180,36 @@ class ViewController: UIViewController, LFLiveSessionDelegate {
172
180
} ( )
173
181
174
182
// 关闭按钮
175
- lazy var closeButton : UIButton = {
176
- let closeButton = UIButton ( frame: CGRect ( x: self . view . frame . width - 10 - 44 , y: 20 , width: 44 , height: 44 ) )
183
+ var closeButton : UIButton = {
184
+ let closeButton = UIButton ( frame: CGRect ( x: UIScreen . main . bounds . width - 10 - 44 , y: 20 , width: 44 , height: 44 ) )
177
185
closeButton. setImage ( UIImage ( named: " close_preview " ) , for: UIControlState ( ) )
178
- closeButton. addTarget ( self , action: #selector( didTappedCloseButton ( _: ) ) , for: UIControlEvents . touchUpInside)
179
186
return closeButton
180
187
} ( )
181
188
182
189
// 摄像头
183
- lazy var cameraButton : UIButton = {
184
- let cameraButton = UIButton ( frame: CGRect ( x: self . view . frame . width - 54 * 2 , y: 20 , width: 44 , height: 44 ) )
190
+ var cameraButton : UIButton = {
191
+ let cameraButton = UIButton ( frame: CGRect ( x: UIScreen . main . bounds . width - 54 * 2 , y: 20 , width: 44 , height: 44 ) )
185
192
cameraButton. setImage ( UIImage ( named: " camra_preview " ) , for: UIControlState ( ) )
186
- cameraButton. addTarget ( self , action: #selector( didTappedCameraButton ( _: ) ) , for: UIControlEvents . touchUpInside)
187
193
return cameraButton
188
194
} ( )
189
195
190
196
// 摄像头
191
- lazy var beautyButton : UIButton = {
192
- let beautyButton = UIButton ( frame: CGRect ( x: self . view . frame . width - 54 * 3 , y: 20 , width: 44 , height: 44 ) )
193
- beautyButton. setImage ( UIImage ( named: " camra_preview " ) , for: UIControlState . selected)
197
+ var beautyButton : UIButton = {
198
+ let beautyButton = UIButton ( frame: CGRect ( x: UIScreen . main . bounds . width - 54 * 3 , y: 20 , width: 44 , height: 44 ) )
199
+ beautyButton. setImage ( UIImage ( named: " camra_beauty " ) , for: UIControlState . selected)
194
200
beautyButton. setImage ( UIImage ( named: " camra_beauty_close " ) , for: UIControlState ( ) )
195
- beautyButton. addTarget ( self , action: #selector( didTappedBeautyButton ( _: ) ) , for: UIControlEvents . touchUpInside)
196
201
return beautyButton
197
202
} ( )
198
203
199
204
// 开始直播按钮
200
- lazy var startLiveButton : UIButton = {
201
- let startLiveButton = UIButton ( frame: CGRect ( x: 30 , y: self . view . frame . height - 50 , width: self . view . frame . width - 10 - 44 , height: 44 ) )
205
+ var startLiveButton : UIButton = {
206
+ let startLiveButton = UIButton ( frame: CGRect ( x: 30 , y: UIScreen . main . bounds . height - 50 , width: UIScreen . main . bounds . width - 10 - 44 , height: 44 ) )
202
207
startLiveButton. layer. cornerRadius = 22
203
208
startLiveButton. setTitleColor ( UIColor . black, for: UIControlState ( ) )
204
209
startLiveButton. setTitle ( " 开始直播 " , for: UIControlState ( ) )
205
210
startLiveButton. titleLabel!. font = UIFont . systemFont ( ofSize: 14 )
206
211
startLiveButton. backgroundColor = UIColor ( colorLiteralRed: 50 , green: 32 , blue: 245 , alpha: 1 )
207
- startLiveButton. addTarget ( self , action: #selector( didTappedStartLiveButton ( _: ) ) , for: UIControlEvents . touchUpInside)
208
212
return startLiveButton
209
213
} ( )
210
-
211
- // 转屏
212
- override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
213
- return UIInterfaceOrientationMask . portrait
214
- }
215
-
216
- override var shouldAutorotate : Bool {
217
- return true
218
- }
219
214
}
220
215
0 commit comments