阿里云移动端播放器高级功能---直播时移

基本介绍

通常都知道直播是无法seek拖动的,那么针对在直播中想回看之前直播过的内容的用户来说,直播时移就能派上用场。我们阿里云播放器支持了直播时移功能,用户能较为方面和快速的使用直播时移的功能。
先来看一下直播时移的介绍:时移直播基于常规的HLS视频直播,直播推流被切分成TS分片,通过HLS协议向播放用户分发,用户请求的m3u8播放文件中包含不断刷新的TS分片地址;对于常规的HLS直播而言,TS分片地址及相应的TS文件并不持久化保存,导致当前时间之前的直播视频内容无法回溯;而对于开通了时移功能的HLS直播而言,TS分片地址及相应TS文件会分别在数据库和OSS中持久化保存最长15天,使得回溯从直播开始时间到当前时间之间的视频内容成为可能。
更多的信息可以参考官网介绍:直播时移

先来看一下时移效果图:
81a5829ec9403c2690d72ca0394364543f1db8e2

时移原理

时移是通过时间轴url(TimeLineUrl) 去实时获取到可以时移的时间范围。在这个时间范围内的直播流,可以往回拖拽,回看之前的内容。拖拽时,播放器内部通过更新直播流的地址,加上起播的时间参数,然后从服务器拉取新的流,达到回看的目的。

接口和示例

Android使用

时移源的设置

AliyunVodPlayer 提供了 AliyunLiveTimeShift 这个类作为直播时移的播放源。其中setUrlsetTimeLineUrl是主要涉及的两个地址。
setUrl:这个设置的是直播流的地址。通过这个地址,播放器播放对应的直播源。
setTimeLineUrl:这个设置的是时移区间的获取地址。播放器将会每隔1分钟调用一次此接口去更新时移时间段。
(如何生成TimeLineUrl,请参考:直播时移

时移信息的获取

时移的获取,跟普通的点播信息获取基本类似,但是有特有的新增接口(Android):
getCurrentLiveTime:获取当前直播的时间点。
getCurrentTime:获取当前播放的时间点。

比如:现在是2019-01-30 14:01:04,播放的时候,往前时移了1分钟。那么:
getCurrentLiveTime = 2019-01-30 14:01:04。
getCurrentTime = 2019-01-30 14:00:04。

示例

//1.创建时移对象
mAliyunLiveTimeShift = new AliyunLiveTimeShift();
long currentSeconds = System.currentTimeMillis() / 1000;

//2.设置直播流地址
mAliyunLiveTimeShift.setUrl("http://pull-videocall.aliyuncs.com/timeline/test.m3u8");

//3.设置时移区间地址。
mAliyunLiveTimeShift.setTimeLineUrl("http://pull-videocall.aliyuncs.com/openapi/timeline/query?lhs_start=1&app=timeline&stream=test&format=ts&lhs_start_unix_s_0="
                + (currentSeconds - 5 * 60) + "&lhs_end_unix_s_0=" + (currentSeconds + 5 * 60));

//4.准备时移源。
mPlayer.prepareAsync(mAliyunLiveTimeShift);

iOS接口和示例

iOS提供了以下接口来使用直播时移:

//直播时间
@property (nonatomic, assign) NSTimeInterval liveTime;

//播放时间
@property (nonatomic, assign) NSTimeInterval currentPlayTime;

//每60秒更新用户时移时间,设置更新区间url后,可以获取
@property (nonatomic, strong) AliyunPlayerVideoTimeShiftModel *timeShiftModel;

//设置直播数据源
- (void)prepareWithLiveTimeUrl:(NSURL *)liveTimeUrl;

//设置时移区间更新url
- (void)setLiveTimeShiftUrl:(NSString*)liveTimeShiftUrl;

//时移到指定的时间
- (void)seekToLiveTime:(NSTimeInterval)startTime;

具体使用过程:

//1.设置时移区间更新url,地址需要用当前时间来进行拼写
NSTimeInterval currentSeconds = [[NSDate date] timeIntervalSince1970]; //秒
    NSString *currentLive = [NSString stringWithFormat:@"http://push-demo.aliyunlive.com/openapi/timeline/query?app=asr&stream=yunxi&format=ts&lhs_start_unix_s_0=%.0f&lhs_end_unix_s_0=%.0f",(currentSeconds - 5 * 60), (currentSeconds + 5 * 60)];
[self.vodPlayer setLiveTimeShiftUrl:currentLive];

//2. 准备直播时移播放地址
[self.vodPlayer prepareWithLiveTimeUrl:[NSURL URLWithString:@"http://push-demo.aliyunlive.com/asr/yunxi.m3u8"]];

//3. 播放成功后可以更新界面UI显示
//开始时间
double start = self.vodPlayer.timeShiftModel.startTime;
//记录总的结束时间
self.vodPlayer.timeShiftModel.endTime

//4. 拖动seek时移区间,通过进度条计算出来具体的时移时间
[self.vodPlayer seekToLiveTime:(int)(n*sender.value+s)];

猜你喜欢

转载自yq.aliyun.com/articles/689912