基本介紹
通常都知道直播是無法seek拖動的,那麼針對在直播中想回看之前直播過的内容的使用者來說,直播時移就能派上用場。我們阿裡雲播放器支援了直播時移功能,使用者能較為方面和快速的使用直播時移的功能。
先來看一下直播時移的介紹:時移直播基于正常的HLS視訊直播,直播推流被切分成TS分片,通過HLS協定向播放使用者分發,使用者請求的m3u8播放檔案中包含不斷重新整理的TS分片位址;對于正常的HLS直播而言,TS分片位址及相應的TS檔案并不持久化儲存,導緻目前時間之前的直播視訊内容無法回溯;而對于開通了時移功能的HLS直播而言,TS分片位址及相應TS檔案會分别在資料庫和OSS中持久化儲存最長15天,使得回溯從直播開始時間到目前時間之間的視訊内容成為可能。
更多的資訊可以參考官網介紹:
直播時移先來看一下時移效果圖:

https://www.atatech.org/articles/131371#1 時移原理
時移是通過時間軸url(TimeLineUrl) 去實時擷取到可以時移的時間範圍。在這個時間範圍内的直播流,可以往回拖拽,回看之前的内容。拖拽時,播放器内部通過更新直播流的位址,加上起播的時間參數,然後從伺服器拉取新的流,達到回看的目的。
https://www.atatech.org/articles/131371#2 接口和示例
https://www.atatech.org/articles/131371#3 Android使用
https://www.atatech.org/articles/131371#4 時移源的設定
AliyunVodPlayer 提供了
AliyunLiveTimeShift
這個類作為直播時移的播放源。其中
setUrl
和
setTimeLineUrl
是主要涉及的兩個位址。
setUrl:這個設定的是直播流的位址。通過這個位址,播放器播放對應的直播源。
setTimeLineUrl:這個設定的是時移區間的擷取位址。播放器将會每隔1分鐘調用一次此接口去更新時移時間段。
(如何生成TimeLineUrl,請參考:
)
https://www.atatech.org/articles/131371#5 時移資訊的擷取
時移的擷取,跟普通的點播資訊擷取基本類似,但是有特有的新增接口(Android):
getCurrentLiveTime:擷取目前直播的時間點。
getCurrentTime:擷取目前播放的時間點。
比如:現在是2019-01-30 14:01:04,播放的時候,往前時移了1分鐘。那麼:
getCurrentLiveTime = 2019-01-30 14:01:04。
getCurrentTime = 2019-01-30 14:00:04。
https://www.atatech.org/articles/131371#6 示例
//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);
https://www.atatech.org/articles/131371#7 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)];
關注 0人關注該