乐视移动直播的集成(四)—— 短视频播放的一种封装

版本记录

版本号 时间
V1.0 2017.07.21

前言

以前做过移动直播项目,做直播的推流和拉流用的是乐视的移动直播业务,集成乐视的移动业务的SDK。前面已经对乐视移动直播进行了简单的介绍,感兴趣的可以看下。这一篇主要讲一下乐视云短视频播放工具类的封装。
1.乐视移动直播的集成(一)
2.乐视移动直播的集成(二)
3.乐视移动直播的集成(三)—— 几个工程中碰到的小问题

短视频播放工具类封装

乐视的云直播业务也支持短视频播放的功能,在使用的时候不是很方便直接使用乐视提供的SDK接口,为了特殊定制自己的项目,是需要简单的封装一个工具类,下面就和大家分享一下,希望对大家有所帮助。

下面直接看代码吧。

1. JJLecVideoVodManager.h
#import 

@class LECStreamRateItem;

@protocol JJLecVideoVodDelegate 

//播放时间回调
- (void) playWithTimeInterval:(NSTimeInterval)timeInterval Duration:(NSTimeInterval)duration;

- (void) vodPlayEnd;

- (void) playWithSuccess:(BOOL)isResult;

@end

@interface JJLecVideoVodManager : NSObject

@property (nonatomic, weak) iddelegate;

//获取播放面板
- (UIView *)videoViewWithType:(int)type;

//切换码率接口
- (void) switchSelectStreamRateItem:(LECStreamRateItem *) selectStreamRateItem;

//播放接口
- (void) playShortVideoVodWithURL:(NSString *)url;

//停止播放接口
- (void) stopVideo;

- (void) stopPlay;

//暂停/继续接口
- (void)pause;

//暂停/继续接口
- (void)resume;

- (void)didClickPause;

//获取视频总时长
- (NSInteger)videoLength;

//调节音量
- (void)setVolume:(NSInteger)volume;

@end
2. JJLecVideoVodManager.m
#import "JJLecVideoVodManager.h"
#import "LECPlayer.h"
#import "SCGIFImageView.h"

@interface JJLecVideoVodManager () 

@property (nonatomic, strong) LECPlayer *player;            //点播对象
@property (nonatomic, strong) UIView *playView;
@property (nonatomic, copy) NSString *playUrl;
@property (nonatomic, strong) SCGIFImageView *loadingView;

@end

@implementation JJLecVideoVodManager

#pragma mark - Override Base Function

- (instancetype)init
{
    if (self = [super init]) {
        _player = [[LECPlayer alloc] init];
        _player.codecCoreType = LECPlayerCodecCoreTypeAVPlayer;
        _player.delegate = self;
    }
    return self;
}

#pragma mark - Lazy Load

- (SCGIFImageView *)loadingView
{
    if (!_loadingView) {
        _loadingView = [[SCGIFImageView alloc] initWithFrame:CGRectMake(0, 0, kJJCommonScreenWidth, 490/2)];
        NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"gifLoading.gif" ofType:nil]];
        [_loadingView setData:data];
        _loadingView.isRecount = YES;
        _loadingView.contentMode = UIViewContentModeCenter;
        [[_player videoView] addSubview:_loadingView];
        [_loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo([_player videoView]);
            make.size.mas_equalTo(CGSizeMake(131, 72));
        }];
    }
    return _loadingView;
}

#pragma mark - Object Public Function

//设置播放面板

- (UIView *)videoViewWithType:(int)type
{
    [self.player videoView].frame = kJJCommonScreenBounds;
    [self showLoading];
    return [self.player videoView];
}

//切换码率接口

- (void)switchSelectStreamRateItem:(LECStreamRateItem *) selectStreamRateItem
{
    if (self.player.selectedStreamRateItem == selectStreamRateItem) {
        return;
    }
    
    if ([self.player switchSelectStreamRateItem:selectStreamRateItem]) {
        DDLogInfo(@"切换成功");
    }
    else {
        DDLogInfo(@"切换失败");
    }
}

//播放接口

- (void)playShortVideoVodWithURL:(NSString *)url
{
    _playUrl = url;
    
    [self.player unregister];
    [self.player registerWithURLString:url completion:^(BOOL result) {
        if (result) {
            NSLog(@"拉流成功");
            [self.player prepare];
            [self.player play];
        }
        else {
            NSLog(@"拉流失败");
        }
    }];
}

//停止播放接口

- (void)stopVideo
{
    [_player unregister];
    [_player stop];
    [_player setVolume:0];
    _player.delegate = self;
    _player = nil;
}

- (void)stopPlay
{
    [_player stop];
}

//暂停/继续接口

- (void)resume
{
    if (_player.playStatus == LECPlayerPlayStatusPaused) {
        [_player resume];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:YES];
        }
    }
    else if (!(_player.playStatus == LECPlayerPlayStatusPlaying)) {
        [_player play];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:YES];
        }
    }
}

- (void)pause
{
    if (_player.playStatus == LECPlayerPlayStatusPlaying){
        [_player pause];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:NO];
        }
    }
}

//获取视频总时长

- (NSInteger)videoLength
{
    return _player.duration;
}

//调节音量

- (void)setVolume:(NSInteger)volume
{
    _player.volume = volume;
}

#pragma mark - Object Private Function

- (void)showLoading
{
    [self.loadingView startAnimating];
    self.loadingView.hidden = NO;
}

- (void)hiddenLoading
{
    [self.loadingView stopAnimating];
    self.loadingView.hidden = YES;
}

- (void)didClickPause
{
    if(_player.playStatus == LECPlayerPlayStatusPlaying) {
        [_player pause];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:NO];
        }
    }
    else if (_player.playStatus == LECPlayerPlayStatusPaused) {
        [_player resume];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:YES];
        }
    }
    else {
        [_player play];
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:YES];
        }
    }
}

#pragma mark - LECPlayerDelegate

//播放器播放状态

- (void)lecPlayer:(LECPlayer *) player
       playerEvent:(LECPlayerPlayEvent) playerEvent
{
    if (playerEvent == LECPlayerPlayEventPrepareDone) {
        DDLogWarn(@"准备结束");
    }
    else if (playerEvent == LECPlayerPlayEventEOS) {
        DDLogWarn(@"播放结束");
        if ([self.delegate respondsToSelector:@selector(vodPlayEnd)]) {
            [self.delegate vodPlayEnd];
        }
    }
    else if (playerEvent == LECPlayerPlayEventGetVideoSize) {
        DDLogWarn(@"视频源Size");
    }
    else if (playerEvent == LECPlayerPlayEventRenderFirstPic) {
        DDLogWarn(@"获取到第一帧");
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:YES];
        }
        [self hiddenLoading];
    }
    else if (playerEvent == LECPlayerPlayEventBufferStart) {
        DDLogWarn(@"开始缓冲");
        [self showLoading];
    }
    else if (playerEvent == LECPlayerPlayEventBufferEnd) {
        DDLogWarn(@"缓冲结束");
        [self hiddenLoading];
    }
    else if (playerEvent == LECPlayerPlayEventSeekComplete) {
        DDLogWarn(@"seek结束");
        [self hiddenLoading];
    }
    else if (playerEvent == LECPlayerPlayEventPlayError) {
        DDLogWarn(@"播放出错");
        [self hiddenLoading];
        if ([_player videoView].superview) {
            [[_player videoView].superview makeToast:@"播放出错"];
        }
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:NO];
        }
    }
    else if (playerEvent == LECPlayerPlayEventDisplayError) {
        DDLogWarn(@"播放出错");
        [self hiddenLoading];
        if ([_player videoView].superview) {
            [[_player videoView].superview makeToast:@"播放出错"];
        }
        if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
            [self.delegate playWithSuccess:NO];
        }
    }
    else if (playerEvent == LECPlayerPlayEventSuspend) {
        DDLogWarn(@"直播结束");
    }
    else if (playerEvent == LECPlayerPlayEventNotStarted) {
        DDLogWarn(@"直播未开始");
    }
}

// 播放器播放时间回调

- (void) lecPlayer:(LECPlayer *) player
          position:(int64_t) position
     cacheDuration:(int64_t) cacheDuration
          duration:(int64_t) duration
{    
    if ([self.delegate respondsToSelector:@selector(playWithTimeInterval:Duration:)]) {
        [self.delegate playWithTimeInterval:position Duration:duration];
        if (position > 0 && duration > 0) {
            if (duration == position) {
                [_player seekToPosition:0];
            }
        }
    }
}

@end

短视频播放工具类的使用

短视频播放工具类的使用包括很多方面,包括播放、暂停和停止等,下面我们就以播放为例说一下这个工具的使用情况。

这个其实很简单了,就是在播放器控制器里面调用自定义工具类的播放解口- (void) playShortVideoVodWithURL:(NSString *)url;就可以了。

[self.manager playShortVideoVodWithURL:self.model.playFileName];

其他功能类似,都是直接调用接口就可以了。

后记

未完,待续~~~

乐视移动直播的集成(四)—— 短视频播放的一种封装_第1张图片
大气之秋

你可能感兴趣的:(乐视移动直播的集成(四)—— 短视频播放的一种封装)