[如果觉得文章有用,可以支持一下放眼直播]
一个最简单的播放器的
//
// ijkPlayerViewController.m
// MediaPlayerDemo
//
// Created by gongjia on 2017/2/24.
//
#import "ijkPlayerViewController.h"
#import "ControlView.h"
#import "MBProgressHUD.h"
#import "IJKMediaPlayer/IJKMediaPlayer.h"
@interface ijkPlayerViewController () <ControlViewDelegate>
@property (nonatomic, strong) ControlView *controlView;
@property(atomic, strong) id<IJKMediaPlayback> player;
@end
@implementation ijkPlayerViewController
{
MBProgressHUD *mbProgressHUD;
}
#define alertMsg(msg) UIAlertView *alertMsg = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertMsg show];
- (void)viewDidLoad {
[super viewDidLoad];
IJKFFOptions *options = [IJKFFOptions optionsByDefault]; //使用默认配置
//硬解
[options setPlayerOptionIntValue:0 forKey:@"videotoolbox"];
//https测试链接: https://vod2.fangyan.tv/c07a0ccbd55e47dc.mp4
NSURL * url = [NSURL URLWithString:@"http://vod2.fangyan.tv/39b943f69814af90.m3u8"];
//初始化播放器,播放在线视频
self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
//设置自适应布局
self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.player.view.frame = self.view.bounds;
//缩放模式为FILL
self.player.scalingMode = IJKMPMovieScalingModeAspectFill;
// 设置自动播放模式
self.player.shouldAutoplay = YES;
self.view.autoresizesSubviews = YES;
[self.view addSubview:self.player.view];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 启动预播放操作
[self.player prepareToPlay];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.player shutdown];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
}
@end
iOS端参数设置(和Android基本一样):
//开启硬件解码
[options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];
// 设置音量大小,256为标准音量。(要设置成两倍音量时则输入512,依此类推)
[options setPlayerOptionIntValue:512 forKey:@"vol"];
// 最大fps
[options setPlayerOptionIntValue:30 forKey:@"max-fps"];
// 跳帧开关,如果cpu解码能力不足,可以设置成5,否则
// 会引起音视频不同步,也可以通过设置它来跳帧达到倍速播放
[options setPlayerOptionIntValue:0 forKey:@"framedrop"];
// 指定最大宽度,我没试过
[options setPlayerOptionIntValue:960 forKey:@"videotoolbox-max-frame-width"];
// 自动转屏开关,我没试过
[options setFormatOptionIntValue:0 forKey:@"auto_convert"];
// 重连次数, 我没试过
[options setFormatOptionIntValue:1 forKey:@"reconnect"];
// 超时时间,timeout参数只对http设置有效,若果你用rtmp设置timeout,ijkplayer内部会忽略timeout参数。rtmp的timeout参数含义和http的不一样。
[options setFormatOptionIntValue:30 * 1000 * 1000 forKey:@"timeout"];
// 帧速率(fps) 我没试过(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
[options setPlayerOptionIntValue:29.97 forKey:@"r"];
e.g. 直播设置:
有几个不同的接口:
setFormatOptionValue
setPlayerOptionIntValue
setCodecOptionIntValue
//如果是rtsp协议,可以优先用tcp(默认是用udp)
[options setFormatOptionValue:@"tcp" forKey:@"rtsp_transport"];
//播放前的探测Size,默认是1M, 改小一点会出画面更快
[options setFormatOptionIntValue:1024 * 16 forKey:@"probesize"];
//播放前的探测时间
[options setFormatOptionIntValue:50000 forKey:@"analyzeduration"];
//软解,更稳定
[options setPlayerOptionIntValue:0 forKey:@"videotoolbox"];
//解码参数,画面更清晰
[options setCodecOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_loop_filter"];
//
[options setCodecOptionIntValue:IJK_AVDISCARD_DEFAULT forKey:@"skip_frame"];
Boolean _isLive = true;
if (_isLive) {
// Param for living
[options setPlayerOptionIntValue:3000 forKey:@"max_cached_duration"]; // 最大缓存大小是3秒,可以依据自己的需求修改
[options setPlayerOptionIntValue:1 forKey:@"infbuf"]; // 无限读
[options setPlayerOptionIntValue:0 forKey:@"packet-buffering"]; // 关闭播放器缓冲
} else {
// Param for playback
[options setPlayerOptionIntValue:0 forKey:@"max_cached_duration"];
[options setPlayerOptionIntValue:0 forKey:@"infbuf"];
[options setPlayerOptionIntValue:1 forKey:@"packet-buffering"];
}
Note: 这里有个比较有用的参数,skip_loop_filter
// for codec option 'skip_loop_filter' and 'skip_frame'
typedef enum IJKAVDiscard {
/* We leave some space between them for extensions (drop some
* keyframes for intra-only or drop just some bidir frames). */
IJK_AVDISCARD_NONE =-16, ///< discard nothing
IJK_AVDISCARD_DEFAULT = 0, ///< discard useless packets like 0 size packets in avi
IJK_AVDISCARD_NONREF = 8, ///< discard all non reference
IJK_AVDISCARD_BIDIR = 16, ///< discard all bidirectional frames
IJK_AVDISCARD_NONKEY = 32, ///< discard all frames except keyframes
IJK_AVDISCARD_ALL = 48, ///< discard all
} IJKAVDiscard;
前面两个都看得懂
第三个是抛弃非参考帧(I帧)
第四个是抛弃B帧
第五个是抛弃除关键帧以外的,比如B,P帧
第六个是抛弃所有的帧,这我就奇怪了,之前Android默认的就是48,难道把所有帧都丢了?
那就没有视频帧了, 所以应该不是这么理解,
应该是skip_loop_filter和skip_frame的对象要过滤哪些帧类型。
skip_loop_filter这个是解码的一个参数,叫环路滤波,
设置成48和0,图像清晰度对比,0比48清楚,理解起来就是,
0是开启了环路滤波,过滤的是大部分,
而48基本没启用环路滤波,所以清晰度更低,但是解码性能开销小
skip_loop_filter(环路滤波)简言之:
a:环路滤波器可以保证不同水平的图像质量。
b:环路滤波器更能增加视频流的主客观质量,同时降低解码器的复杂度。
具体参考:
http://blog.csdn.net/h514434485/article/details/52241778
http://www.cnblogs.com/TaigaCon/p/5500110.html
skip_frame我没完全理解意思,应该是等同上面这个类似。
Android:设置和iOS基本一样,接口不同而已,注意区属性的分类,有的是formart的,有的是别的
比如下面这个OPT_CATEGORY_PLAYER
//可以打开h265硬解;
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1);
//开启mediacodec硬解
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);