ijkplayer 简单使用

上一篇文章ijkPlayer编译、打包、多格式及Https支持中详细介绍了 ijkPlayer 的编译及集成,这里说一下简单的使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#import <IJKMediaFramework/IJKMediaPlayer.h>
@interface PlayerViewController ()
@property (nonatomic,strong) id<IJKMediaPlayback> player;
@property (nonatomic, weak) UIView *disPlayerView;
@property (nonatomic, strong) NSDictionary *resultDict;
@end
@implementation PlayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self createPlayerBaseView];
[self createBtn];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self createPlayerWithUrlStr:@"http://pl-ali.youku.com/playlist/m3u8?vid=XMTUxMTU0NzA3Ng%3D%3D&type=mp4&ups_client_netip=7ccda03e&utid=WvVPj35hEWUDAD1%2F6UmfLEIM&ccode=0590&psid=5d12228feb98c5a249ae6a63df2442a6&duration=6808&expire=18000&drm_type=1&drm_device=10&ups_ts=1526032072&onOff=0&encr=0&ups_key=e063ff139dc55f75c431a5954ae66b59"];
});
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self removeMovieNotificationObservers];
[self.player stop];
[[self.player view] removeFromSuperview];
}
#pragma mark - 按钮
- (void)createBtn {
UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeSystem];
playBtn.backgroundColor = [UIColor orangeColor];
playBtn.frame = CGRectMake(10, 500, 100, 40);
[playBtn setTitle:@"播放/暂停" forState:UIControlStateNormal];
[playBtn addTarget:self action:@selector(playBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playBtn];
}
- (void)playBtnClick {
if (self.player.isPlaying) {
[self.player pause];
}else {
[self.player play];
}
}
- (void)createPlayerBaseView {
UIView *disPlayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width/16.0*9.0)];
self.disPlayerView = disPlayerView;
disPlayerView.backgroundColor = [UIColor blackColor];
[self.view addSubview:disPlayerView];
}
- (void)createPlayerWithUrlStr:(NSString *)urlStr {
NSURL *url = [NSURL URLWithString:urlStr];
IJKFFOptions *options = [IJKFFOptions optionsByDefault];
//非标准桢率会导致音画不同步,所以设定为 15 或者 29.97
[options setOptionValue:@"29.97" forKey:@"max-fps" ofCategory:kIJKFFOptionCategoryPlayer];
//若视频处理不过来,会导致音视频不同步,此时丢掉部分帧
[options setPlayerOptionIntValue:5 forKey:@"framedrop"];
self.player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:options];
UIView *playerView = [self.player view];
NSLog(@"%@", self.player);
playerView.frame = self.disPlayerView.bounds;
[self.disPlayerView insertSubview:playerView atIndex:1];
[self installMovieNotificationObservers];
if (![self.player isPlaying]) {
[self.player prepareToPlay];
}
}
#pragma Selector func
- (void)loadStateDidChange:(NSNotification*)notification {
IJKMPMovieLoadState loadState = _player.loadState;
if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
NSLog(@"LoadStateDidChange: IJKMovieLoadStatePlayThroughOK: %dn",(int)loadState);
}else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {
NSLog(@"loadStateDidChange: IJKMPMovieLoadStateStalled: %dn", (int)loadState);
} else {
NSLog(@"loadStateDidChange: ???: %dn", (int)loadState);
}
}
- (void)moviePlayBackFinish:(NSNotification*)notification {
int reason =[[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
switch (reason) {
case IJKMPMovieFinishReasonPlaybackEnded:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %dn", reason);
break;
case IJKMPMovieFinishReasonUserExited:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %dn", reason);
break;
case IJKMPMovieFinishReasonPlaybackError:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %dn", reason);
break;
default:
NSLog(@"playbackPlayBackDidFinish: ???: %dn", reason);
break;
}
}
- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification {
NSLog(@"mediaIsPrepareToPlayDidChangen");
}
- (void)moviePlayBackStateDidChange:(NSNotification*)notification {
switch (_player.playbackState) {
case IJKMPMoviePlaybackStateStopped:
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: stoped", (int)_player.playbackState);
break;
case IJKMPMoviePlaybackStatePlaying:
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: playing", (int)_player.playbackState);
break;
case IJKMPMoviePlaybackStatePaused:
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: paused", (int)_player.playbackState);
break;
case IJKMPMoviePlaybackStateInterrupted:
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: interrupted", (int)_player.playbackState);
break;
case IJKMPMoviePlaybackStateSeekingForward:
case IJKMPMoviePlaybackStateSeekingBackward: {
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: seeking", (int)_player.playbackState);
break;
}
default: {
NSLog(@"IJKMPMoviePlayBackStateDidChange %d: unknown", (int)_player.playbackState);
break;
}
}
}
#pragma Install Notifiacation
- (void)installMovieNotificationObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadStateDidChange:)
name:IJKMPMoviePlayerLoadStateDidChangeNotification
object:_player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackFinish:)
name:IJKMPMoviePlayerPlaybackDidFinishNotification
object:_player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mediaIsPreparedToPlayDidChange:)
name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:_player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStateDidChange:)
name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
object:_player];
}
#pragma Remove Notifiacation
- (void)removeMovieNotificationObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerLoadStateDidChangeNotification
object:_player];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerPlaybackDidFinishNotification
object:_player];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:_player];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:IJKMPMoviePlayerPlaybackStateDidChangeNotification
object:_player];
}