一般都知道直播是没法seek拖动的,那么针对在直播中想回看以前直播过的内容的用户来讲,直播时移就能派上用场。咱们阿里云播放器支持了直播时移功能,用户能较为方面和快速的使用直播时移的功能。
先来看一下直播时移的介绍:时移直播基于常规的HLS视频直播,直播推流被切分红TS分片,经过HLS协议向播放用户分发,用户请求的m3u8播放文件中包含不断刷新的TS分片地址;对于常规的HLS直播而言,TS分片地址及相应的TS文件并不持久化保存,致使当前时间以前的直播视频内容没法回溯;而对于开通了时移功能的HLS直播而言,TS分片地址及相应TS文件会分别在数据库和OSS中持久化保存最长15天,使得回溯从直播开始时间到当前时间之间的视频内容成为可能。
更多的信息能够参考官网介绍:直播时移html
先来看一下时移效果图:
数据库
时移是经过时间轴url(TimeLineUrl) 去实时获取到能够时移的时间范围。在这个时间范围内的直播流,能够往回拖拽,回看以前的内容。拖拽时,播放器内部经过更新直播流的地址,加上起播的时间参数,而后从服务器拉取新的流,达到回看的目的。api
AliyunVodPlayer 提供了 AliyunLiveTimeShift
这个类做为直播时移的播放源。其中setUrl
和setTimeLineUrl
是主要涉及的两个地址。
setUrl:这个设置的是直播流的地址。经过这个地址,播放器播放对应的直播源。
setTimeLineUrl:这个设置的是时移区间的获取地址。播放器将会每隔1分钟调用一次此接口去更新时移时间段。
(如何生成TimeLineUrl,请参考:直播时移)服务器
时移的获取,跟普通的点播信息获取基本相似,可是有特有的新增接口(Android):
getCurrentLiveTime:获取当前直播的时间点。
getCurrentTime:获取当前播放的时间点。app
好比:如今是2019-01-30 14:01:04,播放的时候,往前时移了1分钟。那么:
getCurrentLiveTime = 2019-01-30 14:01:04。
getCurrentTime = 2019-01-30 14:00:04。ide
//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提供了如下接口来使用直播时移:阿里云
//直播时间 @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;
具体使用过程:atom
//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)];