Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait user click when autoplay fails #481

Merged
merged 1 commit into from
Nov 29, 2024
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
6 changes: 6 additions & 0 deletions web/css/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,9 @@
.stats-bitrate {
min-width: 3.3rem;
}

#play-stream {
color: brown;
align-content: center;
font-size: 200%;
}
4 changes: 4 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
</div>
<div id="stats-overlay"></div>
</div>
<div id="play-stream" hidden>
<span>⏵</span>
<span>Click to play</span>
</div>
</div>

<div id="servers"></div>
Expand Down
47 changes: 39 additions & 8 deletions web/js/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {opts, settings} from 'settings';

const videoEl = document.getElementById('stream')
const mirrorEl = document.getElementById('mirror-stream')
const playEl = document.getElementById('play-stream')

const options = {
volume: 0.5,
Expand All @@ -24,20 +25,36 @@ const state = {
h: 0,
aspect: 4 / 3,
fit: 'contain',
ready: false
ready: false,
autoplayWait: false
}

const mute = (mute) => (videoEl.muted = mute)

const onPlay = () => {
state.ready = true
videoEl.poster = ''
resize(state.w, state.h, state.aspect, state.fit)
useCustomScreen(options.mirrorMode === 'mirror')
}

const play = () => {
videoEl.play()
.then(() => {
state.ready = true
videoEl.poster = ''
resize(state.w, state.h, state.aspect, state.fit)
useCustomScreen(options.mirrorMode === 'mirror')
const promise = videoEl.play()

if (promise === undefined) {
log.error('oh no, the video is not a promise!')
return
}

promise
.then(onPlay)
.catch(error => {
if (error.name === 'NotAllowedError') {
showPlayButton()
} else {
log.error('Playback fail', error)
}
})
.catch(error => log.error('Can\'t autoplay', error))
}

const toggle = (show) => state.screen.toggleAttribute('hidden', show === undefined ? show : !show)
Expand All @@ -51,6 +68,19 @@ const resize = (w, h, aspect, fit) => {
fit !== undefined && (state.screen.style['object-fit'] = fit)
}

const showPlayButton = () => {
state.autoplayWait = true
toggle()
playEl.removeAttribute('hidden')
}

playEl.addEventListener('click', () => {
playEl.setAttribute('hidden', "")
state.autoplayWait = false
play()
toggle()
})

// Track resize even when the underlying media stream changes its video size
videoEl.addEventListener('resize', () => {
recalculateSize()
Expand Down Expand Up @@ -189,6 +219,7 @@ export const stream = {
},
},
play,
showPlayButton,
toggle,
hasDisplay: true,
init,
Expand Down
Loading