主播直播頁(yè)面 LFLiveKit的實(shí)現(xiàn)代碼(github下載) 新功能可參考以下代碼導(dǎo)入 此頁(yè)面為一個(gè)UIView,直接呼上去,呼到實(shí)現(xiàn)直播的Controller上去就可以啦
#import "LFLivePreview.h"
#import "UIControl+YYAdd.h"
#import "UIView+YYAdd.h"
#import "LFLiveKit.h"
inline static NSString *formatedSpeed(float bytes, float elapsed_milli) {
if (elapsed_milli <= 0) {
return @"N/A";
}
if (bytes <= 0) {
return @"0 KB/s";
}
float bytes_per_sec = ((float)bytes) * 1000.f / elapsed_milli;
if (bytes_per_sec >= 1000 * 1000) {
return [NSString stringWithFormat:@"%.2f MB/s", ((float)bytes_per_sec) / 1000 / 1000];
} else if (bytes_per_sec >= 1000) {
return [NSString stringWithFormat:@"%.1f KB/s", ((float)bytes_per_sec) / 1000];
} else {
return [NSString stringWithFormat:@"%ld B/s", (long)bytes_per_sec];
}
}
@interface LFLivePreview ()<LFLiveSessionDelegate>
@property (nonatomic, strong) UIButton *beautyButton;
@property (nonatomic, strong) UIButton *cameraButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIButton *startLiveButton;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) LFLiveDebug *debugInfo;
@property (nonatomic, strong) LFLiveSession *session;
@property (nonatomic, strong) UILabel *stateLabel;
@end
@implementation LFLivePreview
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
//獲取Video權(quán)限
[self requestAccessForVideo];
//獲取Audio權(quán)限
[self requestAccessForAudio];
[self addSubview:self.containerView];
[self.containerView addSubview:self.stateLabel];
[self.containerView addSubview:self.closeButton];
[self.containerView addSubview:self.cameraButton];
[self.containerView addSubview:self.beautyButton];
[self.containerView addSubview:self.startLiveButton];
}
return self;
}
#pragma mark -- Public Method
- (void)requestAccessForVideo {
__weak typeof(self) _self = self;
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusNotDetermined: {
// 許可對(duì)話沒有出現(xiàn)伙菜,發(fā)起授權(quán)許可
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[_self.session setRunning:YES];
});
}
}];
break;
}
case AVAuthorizationStatusAuthorized: {
// 已經(jīng)開啟授權(quán)娜谊,可繼續(xù)
dispatch_async(dispatch_get_main_queue(), ^{
[_self.session setRunning:YES];
});
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
// 用戶明確地拒絕授權(quán)怀浆,或者相機(jī)設(shè)備無(wú)法訪問
break;
default:
break;
}
}
- (void)requestAccessForAudio {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (status) {
case AVAuthorizationStatusNotDetermined: {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
}];
break;
}
case AVAuthorizationStatusAuthorized: {
break;
}
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
break;
default:
break;
}
}
#pragma mark -- LFStreamingSessionDelegate
/** live status changed will callback */
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state {
NSLog(@"liveStateDidChange: %ld", state);
switch (state) {
case LFLiveReady:
_stateLabel.text = @"未連接";
break;
case LFLivePending:
_stateLabel.text = @"連接中";
break;
case LFLiveStart:
_stateLabel.text = @"已連接";
break;
case LFLiveError:
_stateLabel.text = @"連接錯(cuò)誤";
break;
case LFLiveStop:
_stateLabel.text = @"未連接";
break;
default:
break;
}
}
/** live debug info callback */
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug *)debugInfo {
NSLog(@"debugInfo uploadSpeed: %@", formatedSpeed(debugInfo.currentBandwidth, debugInfo.elapsedMilli));
}
/** callback socket errorcode */
- (void)liveSession:(nullable LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode {
NSLog(@"errorCode: %ld", errorCode);
}
#pragma mark -- Getter Setter
- (LFLiveSession *)session {
if (!_session) {
_session = [[LFLiveSession alloc] initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfiguration]];
_session.preView = self;
_session.delegate = self;
}
return _session;
}
- (UIView *)containerView {
if (!_containerView) {
_containerView = [UIView new];
_containerView.frame = self.bounds;
_containerView.backgroundColor = [UIColor clearColor];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return _containerView;
}
- (UILabel *)stateLabel {
if (!_stateLabel) {
_stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 80, 40)];
_stateLabel.text = @"未連接";
_stateLabel.textColor = [UIColor whiteColor];
_stateLabel.font = [UIFont boldSystemFontOfSize:14.f];
}
return _stateLabel;
}
- (UIButton *)closeButton {
if (!_closeButton) {
_closeButton = [UIButton new];
_closeButton.size = CGSizeMake(44, 44);
_closeButton.left = self.width - 10 - _closeButton.width;
_closeButton.top = 20;
[_closeButton setImage:[UIImage imageNamed:@"close_preview"] forState:UIControlStateNormal];
_closeButton.exclusiveTouch = YES;
@weakify(self);
[_closeButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
@strongify(self);
[self stopLive];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
}];
}
return _closeButton;
}
- (UIButton *)cameraButton {
if (!_cameraButton) {
_cameraButton = [UIButton new];
_cameraButton.size = CGSizeMake(44, 44);
_cameraButton.origin = CGPointMake(_closeButton.left - 10 - _cameraButton.width, 20);
[_cameraButton setImage:[UIImage imageNamed:@"camra_preview"] forState:UIControlStateNormal];
_cameraButton.exclusiveTouch = YES;
__weak typeof(self) _self = self;
[_cameraButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
AVCaptureDevicePosition devicePositon = _self.session.captureDevicePosition;
_self.session.captureDevicePosition = (devicePositon == AVCaptureDevicePositionBack) ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
}];
}
return _cameraButton;
}
- (UIButton *)beautyButton {
if (!_beautyButton) {
_beautyButton = [UIButton new];
_beautyButton.size = CGSizeMake(44, 44);
_beautyButton.origin = CGPointMake(_cameraButton.left - 10 - _beautyButton.width, 20);
[_beautyButton setImage:[UIImage imageNamed:@"camra_beauty"] forState:UIControlStateNormal];
[_beautyButton setImage:[UIImage imageNamed:@"camra_beauty_close"] forState:UIControlStateSelected];
_beautyButton.exclusiveTouch = YES;
__weak typeof(self) _self = self;
[_beautyButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
_self.session.beautyFace = !_self.session.beautyFace;
_self.beautyButton.selected = !_self.session.beautyFace;
}];
}
return _beautyButton;
}
- (UIButton *)startLiveButton {
if (!_startLiveButton) {
_startLiveButton = [UIButton new];
_startLiveButton.size = CGSizeMake(self.width - 60, 44);
_startLiveButton.left = 30;
_startLiveButton.bottom = self.height - 50;
_startLiveButton.layer.cornerRadius = _startLiveButton.height/2;
[_startLiveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_startLiveButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[_startLiveButton setTitle:@"開始直播" forState:UIControlStateNormal];
[_startLiveButton setBackgroundColor:[UIColor colorWithRed:50 green:32 blue:245 alpha:1]];
_startLiveButton.exclusiveTouch = YES;
__weak typeof(self) _self = self;
[_startLiveButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
//開始直播的回調(diào)
_self.startLiveButton.selected = !_self.startLiveButton.selected;
if (_self.startLiveButton.selected) {
[_self.startLiveButton setTitle:@"結(jié)束直播" forState:UIControlStateNormal];
LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
//RTMP your server rtmp url
stream.url = @"rtmp://live.hkstv.hk.lxdns.com:1935/live/wwMark";
[_self.session startLive:stream];
} else {
[_self.startLiveButton setTitle:@"開始直播" forState:UIControlStateNormal];
[_self.session stopLive];
}
}];
}
return _startLiveButton;
}
- (void)stopLive {
[self.session stopLive];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者