阿里雲移動端播放器高階功能---直播時移
基本介紹
通常都知道直播是無法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)];
本文為雲棲社群原創內容,未經允許不得轉載。