基本介绍
通常都知道直播是无法 seek 拖动的, 那么针对在直播中想回看之前直播过的内容的用户来说, 直播时移就能派上用场. 我们阿里云播放器支持了直播时移功能, 用户能较为方面和快速的使用直播时移的功能.
先来看一下直播时移的介绍: 时移直播基于常规的 HLS 视频直播, 直播推流被切分成 TS 分片, 通过 HLS 协议向播放用户分发, 用户请求的 m3u8 播放文件中包含不断刷新的 TS 分片地址; 对于常规的 HLS 直播而言, TS 分片地址及相应的 TS 文件并不持久化保存, 导致当前时间之前的直播视频内容无法回溯; 而对于开通了时移功能的 HLS 直播而言, TS 分片地址及相应 TS 文件会分别在数据库和 OSS 中持久化保存最长 15 天, 使得回溯从直播开始时间到当前时间之间的视频内容成为可能.
更多的信息可以参考官网介绍: 直播时移
先来看一下时移效果图:
时移原理
时移是通过时间轴 url(TimeLineUrl) 去实时获取到可以时移的时间范围. 在这个时间范围内的直播流, 可以往回拖拽, 回看之前的内容. 拖拽时, 播放器内部通过更新直播流的地址, 加上起播的时间参数, 然后从服务器拉取新的流, 达到回看的目的.
接口和示例
Android 使用
时移源的设置
AliyunVodPlayer 提供了 AliyunLiveTimeShift 这个类作为直播时移的播放源. 其中 setUrl 和 setTimeLineUrl 是主要涉及的两个地址.
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)];
来源: https://www.cnblogs.com/yunqishequ/p/10477255.html