Skip to content

Commit

Permalink
Wait user click when autoplay fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Nov 29, 2024
1 parent b2e275a commit 6de1828
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
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

0 comments on commit 6de1828

Please sign in to comment.