CNLiveMoviePlayerController.h 8.58 KB
//
//  CNLiveMoviePlayerController.h
//
//  Created by CNLive on 16/8/5.
//  Copyright © 2016年 CNLive. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface CNLiveMoviePlayerController : NSObject

#pragma mark - 视讯云内容相关方法 直接通过节目id播放
/* 初始化点播播放器
 *
 * appId             在open.cnlive.com网站申请得到的appId
 * appKey            在open.cnlive.com网站申请得到的app_key
 * vId               点播Id
 *
 */
- (instancetype)initVodPlayWithAppId:(NSString *)appId
                              appKey:(NSString *)appKey
                                 vId:(NSString *)vId;

/* 初始化直播播放器  只有在completionBlock回调后才能对播放器进行操作
 *
 * appId             在open.cnlive.com网站申请得到的appId
 * appKey            在open.cnlive.com网站申请得到的app_key
 * channelId         直播Id
 * completion        初始化完成
 *
 */
- (instancetype)initLivePlayWithAppId:(NSString *)appId
                               appKey:(NSString *)appKey
                            channelId:(NSString *)channelId
                           completion:(void(^)(void))completionBlock;

/* 切换视角和码率
 *
 * channelId        直播Id
 * rate             清晰度(1、2、3、4)
 *
 */
- (void)changeViewWithChannelId:(NSString *)channelId rate:(NSString *)rate;

/* 切换视频源
 *
 * 当视频不够流畅时可调用此方法
 *
 */
- (void)retry;

#pragma mark - 普通播放器方法
/*
 * 初始化播放器并设置播放地址
 */
- (instancetype)initWithContentURL:(NSURL *)url;

/* 重新启动拉流
 *
 * url 视频播放地址,该地址可以是本地地址或者服务器地址.如果为nil,则使用前一次播放地址
 *
 */
- (void)reload:(NSURL *)aUrl;

/*
 * 播放视频时是否需要自动播放,默认值为YES
 */
@property (nonatomic) BOOL shouldAutoplay;

/* 准备视频播放
 *
 * MPMediaPlaybackIsPreparedToPlayDidChangeNotification, 播放器完成对视频文件的初始化时发送通知
 *
 */
- (void)prepareToPlay;

/*
 * 查询视频准备是否完成(只读)
 */
@property(nonatomic, readonly) BOOL isPreparedToPlay;

// Posted when the prepared state changes of an object conforming to the MPMediaPlayback protocol changes.
// This supersedes MPMoviePlayerContentPreloadDidFinishNotification.
MP_EXTERN NSString *const MPMediaPlaybackIsPreparedToPlayDidChangeNotification NS_DEPRECATED_IOS(3_2, 9_0);

/*
 * 播放当前视频
 */
- (void)play;

/* 暂停播放当前视频
 *
 * 播放器内部监听了UIApplicationWillEnterForegroundNotification通知,该通知发生时如果视频仍然在播放,将自动调用pause暂停当前视频播放
 *
 * 需要APP有后台执行权限,在工程Info.plist中添加后台运行模式,设置为audio。具体是添加UIBackgroundModes项,值为audio
 * 当用户点击home按钮后,播放器进入后台继续读取数据并播放音频
 * 当APP回到前台后,音频继续播放。图像渲染内容保持和音频同步
 * 如果在开启后台运行模式后,需要切换后台暂停,需要监听相关事件并主动调用pause操作
 *
 */
- (void)pause;

/* 结束当前视频的播放
 *
 * 重复多次调用将会导致崩溃
 * 调用stop结束当前播放,如果需要重新播放该视频,需要调用[prepareToPlay]([KSYMediaPlayback prepareToPlay])方法
 * 调用stop方法后,播放器开始进入关闭当前播放的操作,操作完成将发送MPMoviePlayerPlaybackDidFinishNotification通知
 *
 * MPMoviePlayerPlaybackDidFinishNotification, 当播放完成将发送该通知
 *
 */
- (void)stop;

/*
 * 当前播放器是否在播放
 */
- (BOOL)isPlaying;

/* 播放视频的当前时刻,单位为秒
 *
 * 视频正常播放时,如果改变currentPlaybackTime的值,将导致播放行为跳转到新的currentPlaybackTime位置播放
 * 如果在视频未播放前设置currentPlaybackTime的值,将导致播放时刻从currentPlaybackTime位置播放
 *
 */
@property(nonatomic) NSTimeInterval currentPlaybackTime;

/*
 * 当前视频总时长(只读)
 */
@property (nonatomic, readonly) NSTimeInterval duration;

/* 当前视频可播放长度(只读)
 *
 * currentPlaybackTime 标记的是播放器当前已播放的时长。
 * playableDuration 标记的是播放器缓冲的时间,会稍大于currentPlaybackTime,与currentPlaybackTime的差值则是缓冲长度。
 *
 */
@property (nonatomic, readonly) NSTimeInterval playableDuration;

/* 包含视频播放内容的VIEW(只读)
 *
 * 可以通过frame设置view大大小
 * 使用[scalingMode]可以更改视频内容在VIEW中的显示情况
 *
 */
@property (nonatomic, readonly) UIView *view;

/* 当前播放器的播放状态(只读)
 *
 * MPMoviePlaybackStateStopped,           // 播放停止
 * MPMoviePlaybackStatePlaying,           // 正在播放
 * MPMoviePlaybackStatePaused,            // 播放暂停
 * MPMoviePlaybackStateInterrupted,       // 播放被打断
 * MPMoviePlaybackStateSeekingForward,    // 向前seeking中
 * MPMoviePlaybackStateSeekingBackward    // 向后seeking中
 *
 * MPMoviePlayerPlaybackDidFinishNotification,当播放完成时提供通知
 * MPMoviePlayerPlaybackStateDidChangeNotification,当播放状态变化时提供通知
 *
 */
@property (nonatomic, readonly) MPMoviePlaybackState playbackState;

/* 当前网络加载情况(只读)
 *
 * MPMovieLoadStateUnknown        = 0,        // 加载情况未知
 * MPMovieLoadStatePlayable       = 1 << 0,   // 加载完成,可以播放
 * MPMovieLoadStatePlaythroughOK  = 1 << 1,   // 加载完成,如果shouldAutoplay为YES,将自动开始播放
 * MPMovieLoadStateStalled        = 1 << 2,   // 如果视频正在加载中
 *
 * MPMoviePlayerLoadStateDidChangeNotification,当加载状态变化时提供通知
 *
 */
@property (nonatomic, readonly) MPMovieLoadState loadState;

/* 当前缩放显示模式
 *
 * MPMovieScalingModeNone,       // 无缩放
 * MPMovieScalingModeAspectFit,  // 同比适配,某个方向会有黑边
 * MPMovieScalingModeAspectFill, // 同比填充,某个方向的显示内容可能被裁剪
 * MPMovieScalingModeFill        // 满屏填充,与原始视频比例不一致
 *
 */
@property (nonatomic) MPMovieScalingMode scalingMode;

/* 当前视频宽高(只读)
 *
 * 监听MPMovieNaturalSizeAvailableNotification
 * 播放过程中,宽高信息可能会产生更改
 *
 */
@property (nonatomic, readonly) CGSize naturalSize;

// Posted when the playback state changes, either programatically or by the user.
MP_EXTERN NSString * const MPMoviePlayerPlaybackStateDidChangeNotification;

// Posted when movie playback ends or a user exits playback.
MP_EXTERN NSString * const MPMoviePlayerPlaybackDidFinishNotification;

MP_EXTERN NSString * const MPMoviePlayerPlaybackDidFinishReasonUserInfoKey; // NSNumber (MPMovieFinishReason)

// Posted when the network load state changes.
MP_EXTERN NSString * const MPMoviePlayerLoadStateDidChangeNotification;

MP_EXTERN NSString * const MPMovieNaturalSizeAvailableNotification;

MP_EXTERN NSString * const MPMoviePlayerFirstVideoFrameRenderedNotification;

MP_EXTERN NSString * const MPMoviePlayerFirstAudioFrameRenderedNotification;

/* bufferTimeMax指定直播流播放时的最大缓冲时长,单位为秒
 *
 * 当buffer为负数时,关闭直播追赶
 * 该属性仅对直播流有效
 * 默认值为2秒
 *
 */
@property NSTimeInterval bufferTimeMax;

/* 已经加载的数据大小(只读)
 *
 * 已经加载的数据大小,单位是兆
 * 已经加载的全部数据大小,包括音频和视频
 * 数据包括已经播放的,和当前的cache数据
 *
 */
@property (nonatomic, readonly) double readSize;

/* buffer为空时,拉取数据所耗的时长(只读)
 *
 * 当buffer为空时,开始统计。单位为秒
 *
 * 当MPMoviePlayerLoadStateDidChangeNotification 通知发起
 * MPMovieLoadState状态为MPMovieLoadStateStalled 开始计时
 * MPMovieLoadState状态为MPMovieLoadStatePlayable 或者 MPMovieLoadStatePlaythroughOK时,结束计时
 *
 */
@property (nonatomic, readonly) NSTimeInterval bufferEmptyDuration;

/* 发起cache的次数(只读)
 *
 * 当buffer为空时,统计一次,统计的条件为
 *
 * 当MPMoviePlayerLoadStateDidChangeNotification 通知发起
 * MPMovieLoadState 状态为MPMovieLoadStateStalled
 *
 */
@property (nonatomic, readonly) NSInteger bufferEmptyCount;

/* 截图
 *
 * 当前时刻的视频UIImage 图像
 *
 */
- (UIImage *)thumbnailImageAtCurrentTime;

/* 是否开启硬件解码
 *
 * 默认是关闭
 * 只在[prepareToPlay]调用前设置生效
 *
 */
@property(nonatomic) BOOL shouldUseHWCodec;

@end