转载地址: 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 , 可以用上一篇的服务地址进行测试。
延迟很大, 但是可以测试用。