腾讯云的官方文档很多人都看不懂,demo也写的没什么借鉴性?而且还是MRC的,一开始我也抱怨好久,找了半天才弄好,分享出来主要代码
导入头文件
#import <TXLivePush.h>
#import <TXLivePlayer.h>
添加属性
@property (nonatomic, assign)TX_Enum_PlayType playType;
@property (nonatomic, strong)TXLivePush * txLivePublisher;
@property (nonatomic, strong) UIView * backView;
@property (nonatomic, strong) UIView * placeholderView;
@property (nonatomic, strong) UIButton * PlayBtn;
@property (nonatomic, assign) NSInteger placeholderHeight;
@property (nonatomic, strong) TXLivePlayer * txLivePlayer;
主要代码
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//将原点移动到navigationBar
self.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// 16 : 9
_placeholderHeight = selfWidth / 16 * 9;
//设置占位背景
self.placeholderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, selfWidth ,_placeholderHeight)];
_placeholderView.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.placeholderView];
self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, _placeholderHeight, selfWidth, 35)];
[self.view addSubview:_backView];
self.hengPingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_hengPingBtn.frame = CGRectMake(selfWidth - 40 , _placeholderHeight - 40, 40, 40);
[_hengPingBtn setImage:[UIImage imageNamed:@"fullscreen_icon"] forState:(UIControlStateNormal)];
_hengPingBtn.selected = YES;
[_hengPingBtn addTarget:self action:@selector(hengPingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.placeholderView addSubview:self.hengPingBtn];
// PlayBtn
self.PlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_PlayBtn.frame = CGRectMake(0 , _placeholderHeight - 40, 40, 40);
[_PlayBtn setImage:[UIImage imageNamed:@"stop_icon"] forState:(UIControlStateNormal)];
_PlayBtn.selected = YES;
[_PlayBtn addTarget:self action:@selector(PlayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.placeholderView addSubview:self.PlayBtn];
_txLivePlayer = [[TXLivePlayer alloc] init];
[_txLivePlayer setupVideoWidget:CGRectMake(0, 0, 0, 0) containView:self.placeholderView insertIndex:0];
_txLivePlayer.enableHWAcceleration = YES;
TXLivePlayConfig *_config = [[TXLivePlayConfig alloc] init];
//流畅模式
_config.bAutoAdjustCacheTime = NO;
// 播放器缓存时间
_config.cacheTime = 3;
[_txLivePlayer setConfig:_config];
// 全屏
[_txLivePlayer setRenderMode:RENDER_MODE_FILL_SCREEN];
[_txLivePlayer setRenderRotation:HOME_ORIENTATION_DOWN];
// 播放 播放地址
[_txLivePlayer startPlay:@"rtmp://live.hkstv.hk.lxdns.com/live/hks" type:_playType];
}
</ BR>
解释下代码
腾讯云的播放器,是不能直接调整帧
要修改控件的大小和位置,请调整父视图的大小和位置
通过调整占位符的帧来修改播放器帧
//全屏图像铺满屏幕
[_txLivePlayer setRenderMode:RENDER_MODE_FILL_SCREEN];
//家在下面
[_txLivePlayer setRenderRotation:HOME_ORIENTATION_DOWN];
</ br>
// _playTyp代表支持的所有格式:
RTMP直播,FLV直播,FLV点播,HLS点播,MP4点播
[_txLivePlayer startPlay:@"rtmp://live.hkstv.hk.lxdns.com/live/hks" type:_playType];
</ BR>
点击事件和是否横屏判断
#pragma mark ==== 全屏点击事件
- (void)hengPingButtonAction:(UIButton *)sender{
if (sender.selected == YES){
[self.view bringSubviewToFront:self.placeholderView];
[Tools orientationToPortrait:UIInterfaceOrientationLandscapeRight];
self.placeholderView.frame = [UIScreen mainScreen].bounds;
_hengPingBtn.frame = CGRectMake(selfWidth - 40 , selfHeigh - 40, 40, 40);
[_hengPingBtn setImage:[UIImage imageNamed:@"exitfullscreen_icon"] forState:(UIControlStateNormal)];
[_PlayBtn setFrame:CGRectMake(0, selfHeigh - 40 , 40, 40)];
self.navigationController.navigationBarHidden = YES;
sender.selected = NO;
} else {
[Tools orientationToPortrait:UIInterfaceOrientationPortrait];
[_hengPingBtn setImage:[UIImage imageNamed:@"fullscreen_icon"] forState:(UIControlStateNormal)];
self.placeholderView.frame = CGRectMake(0, 0, selfWidth , _placeholderHeight);
_hengPingBtn.frame = CGRectMake(selfWidth - 40 , _placeholderHeight - 40, 40, 40);
[_PlayBtn setFrame:CGRectMake(0, _placeholderHeight - 40, 40, 40)];
self.navigationController.navigationBarHidden = NO;
sender.selected = YES;
}
}
#pragma mark ==== 播放 暂停 事件
- (void)PlayBtnAction:(UIButton *)btn{
if (btn.selected == YES){
[btn setImage:[UIImage imageNamed:@"play_icon"] forState:(UIControlStateNormal)];
// 暂停
[_txLivePlayer pause];
btn.selected = NO;
} else {
[btn setImage:[UIImage imageNamed:@"stop_icon"] forState:(UIControlStateNormal)];
// 恢复
[_txLivePlayer resume];
btn.selected = YES;
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 1;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 0;
}
</ BR>
工具类里的转屏
//强制旋转屏幕
+ (void)orientationToPortrait:(UIInterfaceOrientation)orientation{
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];//前两个参数已被target和selector占用
[invocation invoke];
}
网友评论