首先感谢bilibili开源的IJK框架~~
由于github限制上传文件大小 ,而IJKFramework文件100多兆,所以就把主要封装代码贴出来了:
.h文件
//
// AvPlayView.h
// HQUpgrade
//
// Created by 杨志勇 on 16/9/21.
// Copyright © 2016年 zbwx. All rights reserved.
//
#import <UIKit/UIKit.h>
#define videoHeight 184.0
@interface AvPlayView : UIView
@property (nonatomic, copy) NSString *peopleNum;
@property (nonatomic, assign) BOOL needStop; // 默认no
@property (nonatomic, assign) BOOL playEnable; // 默认yes 标志网络情况
@property (nonatomic, copy) void (^intoLivingRoomHandler)(void);
@property (nonatomic, copy) void (^noLoginPlayOverTimeHandler)(void);
@property (nonatomic, copy) void (^closeHandler)(void);
@property (nonatomic, copy) void (^playErrorHandler)(NSString *);
- (void)prepareWithUrlStr:(NSString *)urlStr;
- (void)startPlay;
- (void)pausePlay;
- (void)stopPlay;
- (BOOL)isPlaying;
@end
.m文件
//
// AvPlayView.m
// HQUpgrade
//
// Created by 杨志勇 on 16/9/21.
// Copyright © 2016年 zbwx. All rights reserved.
//
#import "AvPlayView.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
#import "UIViewExt.h"
//返回颜色
#define kUIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface AvPlayView ()
@property (atomic, retain) IJKFFMoviePlayerController<IJKMediaPlayback> *player;
@end
@implementation AvPlayView {
NSString *_urlStr;
UILabel *_peopleNumLabel;
UIButton *_intoLiveRoomBtn;
NSTimeInterval _currentInterval;
UIButton *_playIconBtn;
UIButton *_closeBtn;
UIImageView *_defalutImg;
UIImageView *_bottomMaskView;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[[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];
_needStop = NO;
_playEnable = YES;
[self addVideoLabelAndButton];
}
return self;
}
- (void)closeBtnAction {
self.closeHandler();
}
- (void)moviePlayBackFinish:(NSNotification*)notification {
int reason =[[[notification userInfo] valueForKey:IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
switch (reason) {
case IJKMPMovieFinishReasonPlaybackEnded:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackEnded: %d\n", reason);
break;
case IJKMPMovieFinishReasonUserExited:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonUserExited: %d\n", reason);
break;
case IJKMPMovieFinishReasonPlaybackError:
NSLog(@"playbackStateDidChange: IJKMPMovieFinishReasonPlaybackError: %d\n", reason);
[self pausePlay];
[self prepareWithUrlStr: _urlStr];
if (_playErrorHandler) {
_playErrorHandler(@"获取不到直播源");
}
break;
default:
NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);
break;
}
}
- (void)moviePlayBackStateDidChange:(NSNotification *)notification {
switch (_player.playbackState) {
case IJKMPMoviePlaybackStateStopped: {
break;
}
case IJKMPMoviePlaybackStatePlaying: {
break;
}
case IJKMPMoviePlaybackStatePaused: {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControl) object:nil];
break;
}
case IJKMPMoviePlaybackStateInterrupted: {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControl) object:nil];
break;
}
case IJKMPMoviePlaybackStateSeekingForward:
case IJKMPMoviePlaybackStateSeekingBackward: {
break;
}
default: {
break;
}
}
}
- (void)loadStateDidChange:(NSNotification*)notification {
IJKMPMovieLoadState loadState = _player.loadState;
if ((loadState & IJKMPMovieLoadStatePlaythroughOK) != 0) {
NSLog(@"LoadStateDidChange: IJKMovieLoadStatePlayThroughOK: %d\n",(int)loadState);
}else if ((loadState & IJKMPMovieLoadStateStalled) != 0) {
} else {
NSLog(@"loadStateDidChange: ???: %d\n", (int)loadState);
}
}
- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification {
NSLog(@"mediaIsPrepareToPlayDidChange\n");
}
- (void)playIconBtnAction:(UIButton *)btn {
if (!_playEnable) {
return;
}
[self startPlay];
}
- (BOOL)isPlaying {
// return [_player isPlaying];
return _playIconBtn.hidden;
}
- (void)refreshMediaControl {
_currentInterval = [_player currentPlaybackTime];
if (_needStop) {
if (_currentInterval >= 120) {
[self stopPlay];
_noLoginPlayOverTimeHandler();
} else {
[self performSelector:@selector(refreshMediaControl) withObject:nil afterDelay:0.5];
}
} else {
[self performSelector:@selector(refreshMediaControl) withObject:nil afterDelay:0.5];
}
}
- (void)startPlay {
if (![_player isPlaying]) {
_playIconBtn.hidden = YES;
[self.player prepareToPlay];
[self.player play];
}
}
- (void)pausePlay {
[_player pause];
_playIconBtn.hidden = NO;
_defalutImg.hidden = NO;
}
- (void)stopPlay {
_defalutImg.hidden = NO;
[_player stop];
[_player shutdown];
_player = nil;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControl) object:nil];
}
- (void)intoLiveRoomClick:(UIButton *)btn {
if (_intoLivingRoomHandler) {
_intoLivingRoomHandler();
}
}
- (void)setPeopleNum:(NSString *)peopleNum {
_peopleNum = peopleNum;
_peopleNumLabel.text = [NSString stringWithFormat: @"观看人数:%@",peopleNum];
}
- (void)prepareWithUrlStr:(NSString *)urlStr {
_currentInterval = 0;
_urlStr = urlStr;
[[_player view] removeFromSuperview];
_player = [[IJKFFMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:urlStr] withOptions:nil];
UIView *playerView = [_player view];
playerView.frame = self.bounds;
playerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:playerView];
[_player setScalingMode: IJKMPMovieScalingModeAspectFit];
[playerView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(tapPlayerViewAction)]];
[self bringSubviewToFront: _defalutImg];
[self bringSubviewToFront: _playIconBtn];
[self bringSubviewToFront: _closeBtn];
[self bringSubviewToFront: _bottomMaskView];
}
- (void)tapPlayerViewAction {
_bottomMaskView.hidden = !_bottomMaskView.hidden;
}
- (void)addVideoLabelAndButton {
_defalutImg = [[UIImageView alloc] initWithFrame: self.bounds];
_defalutImg.image = [UIImage imageNamed: @"av_default"];
[self addSubview: _defalutImg];
_defalutImg.contentMode = UIViewContentModeScaleAspectFill;
_defalutImg.clipsToBounds = YES;
_bottomMaskView = [[UIImageView alloc] initWithFrame: CGRectMake(0, self.height - 31, self.width, 31)];
[self addSubview: _bottomMaskView];
_bottomMaskView.userInteractionEnabled = YES;
_bottomMaskView.image = [UIImage imageNamed: @"av_bottom_mask"];
UIImageView *numIconIV = [[UIImageView alloc] initWithFrame: CGRectMake(15, _bottomMaskView.height / 2 - 7, 12, 14)];
numIconIV.image = [UIImage imageNamed: @"av_count_icon"];
[_bottomMaskView addSubview: numIconIV];
UILabel *numLabel = [[UILabel alloc] initWithFrame: CGRectMake(numIconIV.right + 5, 0, 120, _bottomMaskView.height)];
numLabel.textColor = kUIColorFromRGB(0xffffff);
numLabel.font = [UIFont systemFontOfSize: 11];
_peopleNumLabel = numLabel;
_peopleNumLabel.text = @"";
[_bottomMaskView addSubview:numLabel];
CGFloat btnWidth = 90;
CGFloat btnHeight = _bottomMaskView.height;
UIButton *intoLiveRoomBtn = [UIButton buttonWithType: UIButtonTypeCustom];
intoLiveRoomBtn.frame = CGRectMake(self.width - btnWidth, 0, btnWidth, btnHeight);
[intoLiveRoomBtn setImage: [UIImage imageNamed: @"av_intoRoom_n"] forState: UIControlStateNormal];
[intoLiveRoomBtn setTitle: @"进入直播间" forState: UIControlStateNormal];
intoLiveRoomBtn.titleLabel.font = [UIFont systemFontOfSize: 11];
[intoLiveRoomBtn addTarget: self action: @selector(intoLiveRoomClick:) forControlEvents: UIControlEventTouchUpInside];
[intoLiveRoomBtn setImage:[UIImage imageNamed:@"av_intoRoom_h"] forState:UIControlStateHighlighted];
[intoLiveRoomBtn setTitleColor: kUIColorFromRGB(0xcccccc) forState:UIControlStateHighlighted];
_intoLiveRoomBtn = intoLiveRoomBtn;
[_bottomMaskView addSubview:intoLiveRoomBtn];
intoLiveRoomBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -24, 0, 24);
intoLiveRoomBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 52, 0, -52);
UIButton *closeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
closeBtn.frame = CGRectMake(0, 0, 80, 50);
[closeBtn setImage: [UIImage imageNamed: @"av_close_n"] forState: UIControlStateNormal];
[closeBtn setImage: [UIImage imageNamed: @"av_close_h"] forState: UIControlStateHighlighted];
[self addSubview: closeBtn];
_closeBtn = closeBtn;
[closeBtn setImageEdgeInsets: UIEdgeInsetsMake(-3,-6, 3, 6)];
[closeBtn addTarget: self action: @selector(closeBtnAction) forControlEvents: UIControlEventTouchUpInside];
UIButton *playIconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[playIconBtn setImage:[UIImage imageNamed:@"simple_player_center_play"] forState:UIControlStateNormal];
playIconBtn.frame = self.bounds;
[self addSubview:playIconBtn];
[playIconBtn addTarget:self action:@selector(playIconBtnAction:) forControlEvents:UIControlEventTouchUpInside];
_playIconBtn = playIconBtn;
self.backgroundColor = [UIColor blackColor];
}
@end