3.视频播放
1. Player
The core external interface of the player is PLVMediaPlayerCore, and its external implementation classes are PLVVodMediaPlayer. The differences are as follows:
- PLVMediaPlayerCore: The core player class, providing the full functionality of a pure player without any Polyv business capabilities.
- PLVVodMediaPlayer: A subclass of PLVMediaPlayerCore, implementing features such as picture-in-picture, memory playback, and advertisements with Polyv business capabilities.
Note: For versions below 2.1.x, the core external interface of the player is PLVPlayerCore. Please refer to the actual version for invocation.
The following content provides an introduction to PLVVodMediaPlayer.
2. Initialization
First, create a PLVVodMediaPlayer object on the page. Example code is as follows:
#import <PolyvMediaPlayerSDK/PolyvMediaPlayerSDK.h>
@interface UIViewController ()
@property (nonatomic, strong) PLVVodMediaPlayer *player;
@end
@implementation UIViewController()<
PLVMediaPlayerCoreDelegate,
PLVVodMediaPlayerDelegate
>
- (void)viewDidLoad {
[super viewDidLoad];
PLVVodMediaPlayer *player = [[PLVVodMediaPlayer alloc] init];
[player setupDisplaySuperview:self.view];
self.player = player;
}
@end
Alternatively, you can initialize and configure a URL using the following method, which will automatically start playing the configured URL by default:
/// 播放器初始化
- (instancetype)initWithContentURL:(NSURL *)contentURL;
3. Setting the Data Source
Set the data source by calling the interface setVideo::
- (void)setVideo:(PLVVodMediaVideo *)video
After calling this interface, playback will start automatically by default. You can also control whether to start playback automatically through playback parameter configuration.
Alternatively, you can play the corresponding URL using the following method:
/// 设置URL 播放
- (void)setPlayerURL:(NSURL *)playerURL;
4. Playback Parameter Configuration
Various properties are provided to control the playback mode:
/// 播放模式 视频模式、音频模式
@property (nonatomic, assign) PLVVodMediaPlaybackMode playbackMode;
/// 路由线路,仅对加密视频有效,传入 POVVodVideo 对象中 availableRouteLines 数组的元素
@property (nonatomic, copy) NSString *routeLine;
/// 是否开启记忆播放位置,默认 NO, 开启后从前一播放位置续播
@property (nonatomic, assign) BOOL rememberLastPosition;
/// seek 播放定位类型设置。0 默认,1 精确模式
@property (nonatomic, assign) PLVVodMediaPlaySeekType seekType;
/// 音频播放 seek定位类型设置。0 默认,1 快速模式
@property (nonatomic, assign) PLVVodMediaPlayAudioSeekType audioSeekType;
/// 是否播放片头,默认 NO
@property (nonatomic, assign) BOOL enableTeaser;
PLVMediaPlayerCore The parent class provides some commonly used playback parameters, such as auto-play:
@property (nonatomic, assign) BOOL autoPlay;
5. Playback Control
The player provides a series of playback control interfaces, for example:
/// 暂停 主播放器 播放
- (void)pause;
/// 静音 主播放器
- (void)mute;
/// 取消静音 主播放器
- (void)cancelMute;
/// 开始 主播放器 播放
- (void)play;
/// 切换清晰度
- (void)setPlayQuality:(PLVVodQuality )quality;
/// seek 播放控制, 跳到具体时间点开始播放
- (void)seekToTime:(NSTimeInterval)toTime;
For more control operations, refer to the SDK's PLVVodMediaPlayer and its parent class PLVMediaPlayerCore.
6. Callbacks
Using the PLVVodMediaPlayer requires configuring the following two delegates:
- PLVVodMediaPlayerDelegate: Delegate for VOD player-specific callbacks.
- PLVMediaPlayerCoreDelegate: Delegate for basic player callbacks.
- (PLVVodMediaPlayer *)player{
if (!_player){
_player = [[PLVVodMediaPlayer alloc] init];
_player.coreDelegate = self;
_player.delegateVodMediaPlayer = self;
_player.autoPlay = YES;
_player.rememberLastPosition = YES;
_player.enablePIPInBackground = YES;
}
return _player;
}
The following are the capabilities provided by the callbacks, which can be used to implement corresponding product logic through delegate methods:
@protocol PLVMediaPlayerCoreDelegate <NSObject>
@optional
/// 播放器加载前,回调options配置对象
///
/// @note 播放器在加载前,将触发此回调,并附带 PLVOptions,有自定义配置需求时,可对此对象进行参数设置。
/// 当集成的是 PLVIJKPlayer 时,请按 PLVIJKFFOptions 来处理 options;
/// 当集成的是 IJKMediaFramework 时,请按 IJKFFOptions 来处理 options;
///
/// @param player 播放器对象
/// @param options 播放配置对象
- (PLVOptions *)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerWillLoadWithOptions:(PLVOptions *)options;
/// 播放器 已准备好播放
///
/// @param player 播放器对象
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerIsPreparedToPlay:(BOOL)prepared;
/// 播放器 ’加载状态‘ 发生改变
///
/// @param player 播放器对象
/// @param loadState 播放器加载状态
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerLoadStateDidChange:(PLVPlayerLoadState)loadState;
/// 播放器 ’播放状态‘ 发生改变
///
/// @param player 播放器对象
/// @param playbackState 播放器播放状态
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerPlaybackStateDidChange:(PLVPlaybackState)playbackState;
/// 播放器 播放结束
///
/// @param player 播放器对象
/// @param finishReson 播放结束原因
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerPlaybackDidFinish:(PLVPlayerFinishReason)finishReson;
/// 播放器 已销毁
///
/// @param player 播放器对象
/// @param destroy 播放器销毁
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerDidDestroyed:(BOOL)destroy;
/// 主播放器 ‘SEI信息’ 发生改变
///
/// @param player 播放器对象
/// @param timeStamp 附带的时间戳信息
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player playerSeiDidChanged:(long)timeStamp;
/// 首帧渲染回调
- (void)plvMediaPlayerCore:(PLVMediaPlayerCore *)player firstFrameRendered:(BOOL)rendered;
@end
@protocol PLVVodMediaPlayerDelegate <NSObject>
@optional
/// 点播播放器 发生错误
///
/// @param vodMediaPlayer 点播播放器
/// @param error 错误信息对象 (可能为nil;error.code 可详见 PLVFPlayErrorCodeGenerator.h)
- (void)plvVodMediaPlayer:(PLVVodMediaPlayer *)vodMediaPlayer loadMainPlayerFailureWithError:(NSError * _Nullable)error;
/// 点播播放器 定时返回当前播放进度
///
/// @param vodMediaPlayer 点播播放器
/// @param playedProgress 已播放进度 (0.0 ~ 1.0)
/// @param playedTimeString 当前播放时间点字符串 (示例 "01:23")
/// @param durationTimeString 总时长字符串 (示例 "01:23")
- (void)plvVodMediaPlayer:(PLVVodMediaPlayer *)vodMediaPlayer
playedProgress:(CGFloat)playedProgress
playedTimeString:(NSString *)playedTimeString
durationTimeString:(NSString *)durationTimeString;
/// 画中画状态回调
/// @param vodMediaPlayer 点播播放器
/// @param pipState 画中画状态
- (void)plvVodMediaPlayer:(PLVVodMediaPlayer *)vodMediaPlayer pictureInPictureChangeState:(PLVPictureInPictureState )pipState;
/// 画中画开启失败
/// @param vodMediaPlayer 点播播放器
/// @param error 错误信息
- (void)plvVodMediaPlayer:(PLVVodMediaPlayer *)vodMediaPlayer startPictureInPictureWithError:(NSError *)error;
/// 当前网络状态不佳 回调状态
/// @param poorState 网络不佳指示状态
- (void)plvVodMediaPlayer:(PLVVodMediaPlayer *)vodMediaPlayer poorNetworkState:(BOOL)poorState;
7. Destruction
When the player is no longer needed after playback ends, it should be destroyed:
[self.player clearPlayer];
