简单拉流




转载地址:  https://github.com/SunLiner/MiaowShow        


#import "ViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>


@interface ViewController ()

/** 直播播放器 */
@property (nonatomic, strong) IJKFFMoviePlayerController *moviePlayer;

/** 直播流地址 */
@property (nonatomic, copy  ) NSString   *flv;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _flv = @"rtmp://192.168.8.164:1935/zbcs/room";
    
    [self plarFLV:_flv];
}

#pragma mark - private method

- (void)plarFLV:(NSString *)flv{
    
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    [options setPlayerOptionIntValue:1  forKey:@"videotoolbox"];
    // 帧速率(fps) (可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
    //    [options setPlayerOptionIntValue:29.97 forKey:@"r"];
    
    [options setOptionIntValue:29.97 forKey:@"r" ofCategory:kIJKFFOptionCategoryPlayer];
    [options setOptionIntValue:60 forKey:@"max-fps" ofCategory:kIJKFFOptionCategoryPlayer];
    [options setOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_frame" ofCategory:kIJKFFOptionCategoryCodec];
    [options setOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_loop_filter" ofCategory:kIJKFFOptionCategoryCodec];
    
    [options setOptionIntValue:1 forKey:@"videotoolbox" ofCategory:kIJKFFOptionCategoryPlayer];
    
    // -vol——设置音量大小,256为标准音量。(要设置成两倍音量时则输入512,依此类推
    [options setPlayerOptionIntValue:512 forKey:@"vol"];
    _moviePlayer = [[IJKFFMoviePlayerController alloc] initWithContentURLString:flv withOptions:options];
    _moviePlayer.view.frame = self.view.bounds;
    // 填充fill
    _moviePlayer.scalingMode = IJKMPMovieScalingModeAspectFill;
    // 设置自动播放(必须设置为NO, 防止自动播放, 才能更好的控制直播的状态)
    _moviePlayer.shouldAutoplay = NO;
    // 默认不显示
    _moviePlayer.shouldShowHudView = NO;
    [self.view insertSubview:_moviePlayer.view atIndex:0];
    [_moviePlayer prepareToPlay];
    [_moviePlayer play];
    // 设置监听
    [self initObserver];
}

- (void)initObserver{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateDidChange) name:IJKMPMoviePlayerLoadStateDidChangeNotification object:_moviePlayer];
}


#pragma mark - notify method

- (void)stateDidChange{
    if ((_moviePlayer.loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
        if (!_moviePlayer.isPlaying) {
            [_moviePlayer play];
        }
    }else if (_moviePlayer.loadState & IJKMPMovieLoadStateStalled){ // 网速不佳, 自动暂停状态
       
        NSLog(@"网速不佳");
    }
}

@end

记得添加  IJKMediaFramework.framework , 然后还有 libz.tbd , 可以用上一篇的服务地址进行测试。 

延迟很大, 但是可以测试用。 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值