版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.07.21 |
前言
以前做過移動(dòng)直播項(xiàng)目,做直播的推流和拉流用的是樂視的移動(dòng)直播業(yè)務(wù)辉浦,集成樂視的移動(dòng)業(yè)務(wù)的SDK弄抬。前面已經(jīng)對(duì)樂視移動(dòng)直播進(jìn)行了簡(jiǎn)單的介紹,感興趣的可以看下宪郊。這一篇主要講一下樂視云短視頻播放工具類的封裝掂恕。
1.樂視移動(dòng)直播的集成(一)
2.樂視移動(dòng)直播的集成(二)
3.樂視移動(dòng)直播的集成(三)—— 幾個(gè)工程中碰到的小問題
短視頻播放工具類封裝
樂視的云直播業(yè)務(wù)也支持短視頻播放的功能,在使用的時(shí)候不是很方便直接使用樂視提供的SDK接口弛槐,為了特殊定制自己的項(xiàng)目懊亡,是需要簡(jiǎn)單的封裝一個(gè)工具類,下面就和大家分享一下乎串,希望對(duì)大家有所幫助斋配。
下面直接看代碼吧。
1. JJLecVideoVodManager.h
#import <Foundation/Foundation.h>
@class LECStreamRateItem;
@protocol JJLecVideoVodDelegate <NSObject>
//播放時(shí)間回調(diào)
- (void) playWithTimeInterval:(NSTimeInterval)timeInterval Duration:(NSTimeInterval)duration;
- (void) vodPlayEnd;
- (void) playWithSuccess:(BOOL)isResult;
@end
@interface JJLecVideoVodManager : NSObject
@property (nonatomic, weak) id<JJLecVideoVodDelegate>delegate;
//獲取播放面板
- (UIView *)videoViewWithType:(int)type;
//切換碼率接口
- (void) switchSelectStreamRateItem:(LECStreamRateItem *) selectStreamRateItem;
//播放接口
- (void) playShortVideoVodWithURL:(NSString *)url;
//停止播放接口
- (void) stopVideo;
- (void) stopPlay;
//暫停/繼續(xù)接口
- (void)pause;
//暫停/繼續(xù)接口
- (void)resume;
- (void)didClickPause;
//獲取視頻總時(shí)長(zhǎng)
- (NSInteger)videoLength;
//調(diào)節(jié)音量
- (void)setVolume:(NSInteger)volume;
@end
2. JJLecVideoVodManager.m
#import "JJLecVideoVodManager.h"
#import "LECPlayer.h"
#import "SCGIFImageView.h"
@interface JJLecVideoVodManager () <LECPlayerDelegate>
@property (nonatomic, strong) LECPlayer *player; //點(diǎn)播對(duì)象
@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
//設(shè)置播放面板
- (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];
}
//暫停/繼續(xù)接口
- (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];
}
}
}
//獲取視頻總時(shí)長(zhǎng)
- (NSInteger)videoLength
{
return _player.duration;
}
//調(diào)節(jié)音量
- (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
//播放器播放狀態(tài)
- (void)lecPlayer:(LECPlayer *) player
playerEvent:(LECPlayerPlayEvent) playerEvent
{
if (playerEvent == LECPlayerPlayEventPrepareDone) {
DDLogWarn(@"準(zhǔn)備結(jié)束");
}
else if (playerEvent == LECPlayerPlayEventEOS) {
DDLogWarn(@"播放結(jié)束");
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(@"緩沖結(jié)束");
[self hiddenLoading];
}
else if (playerEvent == LECPlayerPlayEventSeekComplete) {
DDLogWarn(@"seek結(jié)束");
[self hiddenLoading];
}
else if (playerEvent == LECPlayerPlayEventPlayError) {
DDLogWarn(@"播放出錯(cuò)");
[self hiddenLoading];
if ([_player videoView].superview) {
[[_player videoView].superview makeToast:@"播放出錯(cuò)"];
}
if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
[self.delegate playWithSuccess:NO];
}
}
else if (playerEvent == LECPlayerPlayEventDisplayError) {
DDLogWarn(@"播放出錯(cuò)");
[self hiddenLoading];
if ([_player videoView].superview) {
[[_player videoView].superview makeToast:@"播放出錯(cuò)"];
}
if ([self.delegate respondsToSelector:@selector(playWithSuccess:)]) {
[self.delegate playWithSuccess:NO];
}
}
else if (playerEvent == LECPlayerPlayEventSuspend) {
DDLogWarn(@"直播結(jié)束");
}
else if (playerEvent == LECPlayerPlayEventNotStarted) {
DDLogWarn(@"直播未開始");
}
}
// 播放器播放時(shí)間回調(diào)
- (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
短視頻播放工具類的使用
短視頻播放工具類的使用包括很多方面灌闺,包括播放、暫停和停止等坏瞄,下面我們就以播放為例說一下這個(gè)工具的使用情況桂对。
這個(gè)其實(shí)很簡(jiǎn)單了,就是在播放器控制器里面調(diào)用自定義工具類的播放解口- (void) playShortVideoVodWithURL:(NSString *)url;
就可以了鸠匀。
[self.manager playShortVideoVodWithURL:self.model.playFileName];
其他功能類似蕉斜,都是直接調(diào)用接口就可以了。
后記
未完缀棍,待續(xù)~~~