1:下载ijkplayer包 https://github.com/Bilibili/ijkplayer。
2:解压 ijkplayer-master, cd 到ijkplayer-master文件夹,执行init-ios.sh,等待下载ffmpeg结束。
3.在 ios/compile-ffmpeg.sh 中将FF_ALL_ARCHS_IOS8_SDK="armv7 i386 x86_64"
修改为:FF_ALL_ARCHS_IOS8_SDK="arm64 i386 x86_64"
4.在 ijkplayer-master 下的 config 中,打开 module-lite.sh
将 export COMMON_FF_CFG_FLAGS="COMMON_FF_CFG_FLAGS --enable-protocol=rtp"
并添加 export COMMON_FF_CFG_FLAGS="COMMON_FF_CFG_FLAGS --enable-decoder=mjpeg"
export COMMON_FF_CFG_FLAGS="”被转义了,老铁们看下图自己补充。
5.cd 到ijkplayer-master文件夹下的ios文件夹执行: ./compile-ffmpeg.sh clean 和 ./compile-ffmpeg.sh all。
6.运行ios/IJKMediaPlayer.会在,"config.h" 中 "armv7/config.h"
将编译后的文件拉到自己项目,导入对应包:
使用:
//
// LiveController.m
// Bluetooth
//
// Created by 姬拉 on 2018/9/3.
// Copyright © 2018年科技. All rights reserved.
//
#import "LiveController.h"
#import "View+Tools.h"
@interface LiveController ()
@property(nonatomic,strong) NSURL *url; //流媒体播放地址
@property(nonatomic,retain) id ijkPlayer; //播放器
@property(nonatomic,strong) UIView *playView; //播放区域
@end
@implementation LiveController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 拉流地址
self.url= [NSURL URLWithString:@"rtsp://192.168.3.152:8000/h.264"];
//调整参数
IJKFFOptions *options = [IJKFFOptions optionsByDefault];
[options setPlayerOptionIntValue:30 forKey:@"max-fps"];
[options setPlayerOptionIntValue:30 forKey:@"r"];
//跳帧开关
[options setPlayerOptionIntValue:1 forKey:@"framedrop"];
[options setPlayerOptionIntValue:0 forKey:@"start-on-prepared"];
[options setPlayerOptionIntValue:0 forKey:@"http-detect-range-support"];
[options setPlayerOptionIntValue:48 forKey:@"skip_loop_filter"];
[options setPlayerOptionIntValue:0 forKey:@"packet-buffering"];
[options setPlayerOptionIntValue:2000000 forKey:@"analyzeduration"];
[options setPlayerOptionIntValue:25 forKey:@"min-frames"];
[options setPlayerOptionIntValue:1 forKey:@"start-on-prepared"];
[options setCodecOptionIntValue:8 forKey:@"skip_frame"];
[options setFormatOptionValue:@"nobuffer" forKey:@"fflags"];
[options setFormatOptionValue:@"8192" forKey:@"probsize"];
//自动转屏开关
[options setFormatOptionIntValue:0 forKey:@"auto_convert"];
//重连次数
[options setFormatOptionIntValue:1 forKey:@"reconnect"];
//开启硬解码
[options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];
//ijk播放器
self.ijkPlayer = [[IJKFFMoviePlayerController alloc] initWithContentURL:self.url withOptions:options];
//播放区域
self.playView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20, 400)];
self.playView.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.playView];
UIView *playingView = [self.ijkPlayer view];
playingView.frame = self.playView.bounds;
//playingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.playView insertSubview:playingView atIndex:1];
//
[self.ijkPlayer setScalingMode:IJKMPMovieScalingModeFill];
[self installMovieNotificationObservers];
if(![self.ijkPlayer isPlaying]){
[self.ijkPlayer prepareToPlay];
}
UILabel*playlb=[[UILabel alloc]initWithFrame:CGRectMake(10, 500, 60, 60)];
[self.view addSubview:playlb];
playlb.backgroundColor=[UIColor redColor];
playlb.text=@"play";
[playlb addTarget:self action:@selector(fuck)];
}
-(void)fuck
{
[self.ijkPlayer pause];
[self.ijkPlayer stop];
[self.ijkPlayer play];
}
//network load state changes
- (void)loadStateDidChange:(NSNotification *)notification{
IJKMPMovieLoadState loadState = self.ijkPlayer.loadState;
NSLog(@"LoadStateDidChange : %d",(int)loadState);
}
//when movie playback ends or a user exits playback.
- (void)moviePlayBackFinish:(NSNotification *)notification{
int reason = [[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
NSLog(@"playBackFinish : %d",reason);
}
//
- (void)mediaIsPreparedToPlayDidChange:(NSNotification *)notification{
NSLog(@"mediaIsPrepareToPlayDidChange");
}
// when the playback state changes, either programatically or by the user
- (void)moviePlayBackStateDidChange:(NSNotification *)notification{
switch (_ijkPlayer.playbackState) {
case IJKMPMoviePlaybackStateStopped:
NSLog(@"playBackState %d: stoped", (int)self.ijkPlayer.playbackState);
break;
case IJKMPMoviePlaybackStatePlaying:
NSLog(@"playBackState %d: playing", (int)self.ijkPlayer.playbackState);
break;
case IJKMPMoviePlaybackStatePaused:
NSLog(@"playBackState %d: paused", (int)self.ijkPlayer.playbackState);
break;
case IJKMPMoviePlaybackStateInterrupted:
NSLog(@"playBackState %d: interrupted", (int)self.ijkPlayer.playbackState);
break;
case IJKMPMoviePlaybackStateSeekingForward:
break;
case IJKMPMoviePlaybackStateSeekingBackward:
break;
default:
break;
}
}
- (void)installMovieNotificationObservers{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateDidChange:)
name:IJKMPMoviePlayerLoadStateDidChangeNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackFinish:)
name:IJKMPMoviePlayerPlaybackDidFinishNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mediaIsPreparedToPlayDidChange:)
name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStateDidChange:)
name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
object:self.ijkPlayer];
}
- (void)removeMovieNotificationObservers{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerLoadStateDidChangeNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerPlaybackDidFinishNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:self.ijkPlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
object:self.ijkPlayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadData
{
}
@end