点播播放器

本文介绍iOS应用端,点播播放器的使用流程。

使用流程

  1. 创建点播播放器。

  2. 配置播放器的渲染窗口及设置播放回调。

  3. 根据需求配置播放器的其他参数。例如:软硬解码偏好、视频画面模式等。

  4. 设置播放数据源。

    1. 按文件播放设备录像:调用QueryDeviceVodUrl接口,获取本地NVR设备或IPC设备的存储卡中,指定录像文件名的录像文件rtmp播放地址,并作为数据源设置给播放器。

    2. 按时间播放设备录像:调用QueryDeviceVodUrlByTime接口,获取本地NVR设备或IPC设备存储卡中,指定录像时间范围的录像文件rtmp播放地址,并作为数据源设置给播放器。

    3. 按文件播放云存录像:调用QueryRecordUrl接口查询云端录像文件的hls播放地址,并作为数据源设置给播放器。

  5. 开始播放。

  6. 暂停、恢复、seek操作。

  7. 停止播放。

  8. 销毁点播播放器。

使用示例

设备录像点播

// 构造播放器实例
self.player = [[LVVodPlayer alloc] init];
// 设置必要的监听
self.player.vodPlayerDelegate = self;
...
#pragma mark LVVodPlayerDelegate
- (void) onVodPlayerError:(LVVodPlayer *_Nonnull)player error:(NSError *_Nonnull)error{
    [self appendInfoText:[NSString stringWithFormat:@"播放出错: %@\n", error]];
}

- (void) onVodPlayerStateChange:(LVVodPlayer *_Nonnull)player playerState:(LVPlayerState)state{
    [self appendInfoText:[NSString stringWithFormat:@"状态变更: %d\n", state]];
}

- (void) onVodPlayerRenderedFirstFrame:(LVVodPlayer *_Nonnull)player elapsedTimeInMs:(NSInteger)elapsedTimeInMs{
    [self appendInfoText:[NSString stringWithFormat:@"首帧出图耗时: %ldms\n", (long)elapsedTimeInMs]];
    [self appendInfoText:[self.player getStatisticsInfo]];
}

- (void) onVodPlayerVideoSizeChanged:(LVVodPlayer *_Nonnull)player width:(NSInteger)width height:(NSInteger)height{
    [self appendInfoText:[NSString stringWithFormat:@"图像大小变化: w=%ld h=%ld\n", (long)width, (long)height]];
}

- (void) onVodPlayerStandardSeiInfoUpdate:(LVVodPlayer *_Nonnull)player sei:(NSData*_Nonnull)data timeStamp:(NSInteger)timeStamp{
    [self appendInfoText:[NSString stringWithFormat:@"sei: %lu %ld\n", (unsigned long)data.length, timeStamp]];
}

- (void) onVodPlayerVideoJitterBufferEmpty:(LVLivePlayer *_Nonnull)player{
}

- (void)onVodPlayerCompletion:(LVVodPlayer * _Nonnull)player {
    [self appendInfoText:@"播放结束"];
}
...
// 创建LVGlkView,放在scrollview中具备画面缩放能力
self.lvGlkView = [[LVGlkView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)];;
[self.scrollView addSubview:self.lvGlkView];
[self.glkView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(@(0));
    make.centerY.equalTo(@(0));
    make.height.equalTo(self.scrollView.mas_height);
    make.width.equalTo(self.scrollView.mas_width);
}];
// 为播放器设置用于画面渲染的窗口
[self.player setWindow:self.lvGlkView];

...
// 设置数据源:从云服务获取的rtmp点播地址及解密密钥
[self.player setDataSource:@"rtmp://xx.xx.xx.xx/vod/xxx" isEncrypted:(YES) decryptIvBase64:(@"xxxxxxx") decryptKeyBase64:@"xxxxxxx"];
// 设置解码策略为硬解码优先
[self.player setDecoderStrategy:LV_DECODER_STRATEGY_HARDWARE_FIRST];
// 设置画面模式为保持宽高
[self.player setVideoScalingMode:LV_MEDIA_VIDEO_SCALING_MODE_FIT];
...
// 开始播放
[self.player start];
...
// 暂停
[self.player pause];
...
// 恢复
[self.player resume];
...
// 停止播放
[self.player stop];

云存录像点播

// 构造播放器实例
self.player = [[LVVodPlayer alloc] init];
// 设置必要的监听
self.player.vodPlayerDelegate = self;
...
#pragma mark LVVodPlayerDelegate
- (void) onVodPlayerError:(LVVodPlayer *_Nonnull)player error:(NSError *_Nonnull)error{
    [self appendInfoText:[NSString stringWithFormat:@"播放出错: %@\n", error]];
}

- (void) onVodPlayerStateChange:(LVVodPlayer *_Nonnull)player playerState:(LVPlayerState)state{
    [self appendInfoText:[NSString stringWithFormat:@"状态变更: %d\n", state]];
}

- (void) onVodPlayerRenderedFirstFrame:(LVVodPlayer *_Nonnull)player elapsedTimeInMs:(NSInteger)elapsedTimeInMs{
    [self appendInfoText:[NSString stringWithFormat:@"首帧出图耗时: %ldms\n", (long)elapsedTimeInMs]];
    [self appendInfoText:[self.player getStatisticsInfo]];
}

- (void) onVodPlayerVideoSizeChanged:(LVVodPlayer *_Nonnull)player width:(NSInteger)width height:(NSInteger)height{
    [self appendInfoText:[NSString stringWithFormat:@"图像大小变化: w=%ld h=%ld\n", (long)width, (long)height]];
}

- (void) onVodPlayerStandardSeiInfoUpdate:(LVVodPlayer *_Nonnull)player sei:(NSData*_Nonnull)data timeStamp:(NSInteger)timeStamp{
    [self appendInfoText:[NSString stringWithFormat:@"sei: %lu %ld\n", (unsigned long)data.length, timeStamp]];
}

- (void) onVodPlayerVideoJitterBufferEmpty:(LVLivePlayer *_Nonnull)player{
}

- (void)onVodPlayerCompletion:(LVVodPlayer * _Nonnull)player {
    [self appendInfoText:@"播放结束"];
}
...
// 创建LVGlkView,放在scrollview中具备画面缩放能力
self.lvGlkView = [[LVGlkView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)];;
[self.scrollView addSubview:self.lvGlkView];
[self.glkView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(@(0));
    make.centerY.equalTo(@(0));
    make.height.equalTo(self.scrollView.mas_height);
    make.width.equalTo(self.scrollView.mas_width);
}];
// 为播放器设置用于画面渲染的窗口
[self.player setWindow:self.lvGlkView];

...
// 设置数据源:从云服务获取的云存hls播放地址
[self.player setDataSource:@"https://xx.xx.xx.xx/hls/xxx.m3u8"];
// 设置解码策略为硬解码优先
[self.player setDecoderStrategy:LV_DECODER_STRATEGY_HARDWARE_FIRST];
// 设置画面模式为保持宽高
[self.player setVideoScalingMode:LV_MEDIA_VIDEO_SCALING_MODE_FIT];
...
// 开始播放
[self.player start];
...
// 暂停
[self.player pause];
...
// 恢复
[self.player resume];
...
// 停止播放
[self.player stop];