iOS 视频开发腾讯云直播iOS_Skill_Collect

iOS端 十分钟简单实现腾讯云直播

2017-02-27  本文已影响3763人  5a9c6f44e578

首先先说一下腾讯云直播是不需要key什么的,当初看了一上午才发现不用也能播

Paste_Image.png

</br>

Paste_Image.png

</br>

添加库
</br>


Paste_Image.png

</br>


Paste_Image.png

BitCode 可以理解为瘦身功能,它能判断出使用者的设备是32位和64位,删掉不需要的内容
还能判断出设备尺寸,不下载多余尺寸的图片
详细了解可以看这篇文章
http://www.jianshu.com/p/f42a33f5eb61

</br>
考虑到视屏需要转屏,而其他页面不需要,在AppDelegate.h里面添加个属性来判断这个页面是否需要旋转

Paste_Image.png

</br>
.m中添加

Paste_Image.png Paste_Image.png

直播页面

引用文件

Paste_Image.png

</br>
属性


Paste_Image.png

</br>

- (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>

解释下代码

腾讯云的播放器,是不能直接调整frame
要修改控件的大小及位置,请调整父view的大小及位置
通过调整placeholderView 的frame 来修改 播放器frame

</br>
// 全屏 图像铺满屏幕

 [_txLivePlayer setRenderMode:RENDER_MODE_FILL_SCREEN];

</br>
// home在下面

[_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];
}

</br>
</br>
// 附上 demo 缺少 TXRTMPSDK.framework
https://git.oschina.net/wwwzz/LiveRadioDemo

上一篇 下一篇

猜你喜欢

热点阅读