Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Fix/allow video autoplay without click #381

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Frontend/library/src/VideoPlayer/StreamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class StreamController {
constructor(videoElementProvider: VideoPlayer) {
this.videoElementProvider = videoElementProvider;
this.audioElement = document.createElement('Audio') as HTMLAudioElement;
this.videoElementProvider.setAudioElement(this.audioElement);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion Frontend/library/src/VideoPlayer/VideoPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare global {
export class VideoPlayer {
private config: Config;
private videoElement: HTMLVideoElement;
private audioElement?: HTMLAudioElement;
private orientationChangeTimeout: number;
private lastTimeResized = new Date().getTime();

Expand Down Expand Up @@ -52,8 +53,11 @@ export class VideoPlayer {
);
};

// set play for video
// set play for video (and audio)
this.videoElement.onclick = () => {
if (this.audioElement != undefined && this.audioElement.paused) {
this.audioElement.play();
}
if (this.videoElement.paused) {
this.videoElement.play();
}
Expand All @@ -70,6 +74,10 @@ export class VideoPlayer {
);
}

public setAudioElement(audioElement: HTMLAudioElement) : void {
this.audioElement = audioElement;
}

/**
* Sets up the video element with any application config and plays the video element.
* @returns A promise for if playing the video was successful or not.
Expand Down
42 changes: 23 additions & 19 deletions Frontend/library/src/WebRtcPlayer/WebRtcPlayerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,26 +1109,30 @@ export class WebRtcPlayerController {
this.pixelStreaming.dispatchEvent(new PlayStreamEvent());

if (this.streamController.audioElement.srcObject) {
this.streamController.audioElement.muted =
this.config.isFlagEnabled(Flags.StartVideoMuted);
const startMuted = this.config.isFlagEnabled(Flags.StartVideoMuted)
this.streamController.audioElement.muted = startMuted;

this.streamController.audioElement
.play()
.then(() => {
this.playVideo();
})
.catch((onRejectedReason) => {
Logger.Log(Logger.GetStackTrace(), onRejectedReason);
Logger.Log(
Logger.GetStackTrace(),
'Browser does not support autoplaying video without interaction - to resolve this we are going to show the play button overlay.'
);
this.pixelStreaming.dispatchEvent(
new PlayStreamRejectedEvent({
reason: onRejectedReason
})
);
});
if (startMuted) {
this.playVideo();
} else {
this.streamController.audioElement
.play()
.then(() => {
this.playVideo();
})
.catch((onRejectedReason) => {
Logger.Log(Logger.GetStackTrace(), onRejectedReason);
Logger.Log(
Logger.GetStackTrace(),
'Browser does not support autoplaying video without interaction - to resolve this we are going to show the play button overlay.'
);
this.pixelStreaming.dispatchEvent(
new PlayStreamRejectedEvent({
reason: onRejectedReason
})
);
});
}
} else {
this.playVideo();
}
Expand Down
Loading