+ return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
+ }
+
+ private async loadVideoAndBuildPlayer (uuid: string) {
+ const res = await this.loadVideo(uuid)
+ if (res === undefined) return
+
+ return this.buildVideoPlayer(res.videoResponse, res.captionsPromise)
+ }
+
+ private nextVideoTitle () {
+ const next = this.getNextPlaylistElement()
+ if (!next) return ''
+
+ return next.video.name
+ }
+
+ private getNextPlaylistElement (position?: number): VideoPlaylistElement {
+ if (!position) position = this.currentPlaylistElement.position + 1
+
+ if (position > this.playlist.videosLength) {
+ return undefined
+ }
+
+ const next = this.playlistElements.find(e => e.position === position)
+
+ if (!next || !next.video) {
+ return this.getNextPlaylistElement(position + 1)
+ }
+
+ return next
+ }
+
+ private async buildVideoPlayer (videoResponse: Response, captionsPromise: Promise<Response>) {
+ let alreadyHadPlayer = false
+
+ if (this.player) {
+ this.player.dispose()
+ alreadyHadPlayer = true
+ }
+
+ this.playerElement = document.createElement('video')
+ this.playerElement.className = 'video-js vjs-peertube-skin'
+ this.playerElement.setAttribute('playsinline', 'true')
+ this.wrapperElement.appendChild(this.playerElement)
+
+ const videoInfoPromise = videoResponse.json()
+ .then((videoInfo: VideoDetails) => {
+ if (!alreadyHadPlayer) this.loadPlaceholder(videoInfo)
+
+ return videoInfo
+ })
+
+ const [ videoInfo, serverTranslations, captionsResponse, config, PeertubePlayerManagerModule ] = await Promise.all([
+ videoInfoPromise,
+ this.translationsPromise,
+ captionsPromise,
+ this.configPromise,
+ this.PeertubePlayerManagerModulePromise
+ ])
+
+ const PeertubePlayerManager = PeertubePlayerManagerModule.PeertubePlayerManager
+ const videoCaptions = await this.buildCaptions(serverTranslations, captionsResponse)
+
+ this.loadParams(videoInfo)
+
+ const playlistPlugin = this.currentPlaylistElement
+ ? {
+ elements: this.playlistElements,
+ playlist: this.playlist,
+
+ getCurrentPosition: () => this.currentPlaylistElement.position,
+
+ onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => {
+ this.currentPlaylistElement = videoPlaylistElement
+
+ this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
+ .catch(err => console.error(err))
+ }
+ }
+ : undefined
+
+ const options: PeertubePlayerManagerOptions = {
+ common: {
+ // Autoplay in playlist mode
+ autoplay: alreadyHadPlayer ? true : this.autoplay,
+ controls: this.controls,
+ muted: this.muted,
+ loop: this.loop,
+ captions: videoCaptions.length !== 0,
+ startTime: this.startTime,
+ stopTime: this.stopTime,
+ subtitle: this.subtitle,
+
+ nextVideo: () => this.autoplayNext(),
+ playlist: playlistPlugin,
+
+ videoCaptions,
+ inactivityTimeout: 2500,
+ videoViewUrl: this.getVideoUrl(videoInfo.uuid) + '/views',
+
+ playerElement: this.playerElement,
+ onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,
+
+ videoDuration: videoInfo.duration,
+ enableHotkeys: true,
+ peertubeLink: this.peertubeLink,
+ poster: window.location.origin + videoInfo.previewPath,
+ theaterButton: false,
+
+ serverUrl: window.location.origin,
+ language: navigator.language,
+ embedUrl: window.location.origin + videoInfo.embedPath
+ },
+
+ webtorrent: {
+ videoFiles: videoInfo.files