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

Fix: Enable Play/Pause Functionality on Tapping and Space Button for Video Playback #1270

Merged
Merged
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
58 changes: 41 additions & 17 deletions src/components/App/SideBar/SidebarSubView/MediaPlayer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Props = {
const MediaPlayerComponent: FC<Props> = ({ hidden }) => {
const playerRef = useRef<ReactPlayer | null>(null)
const wrapperRef = useRef<HTMLDivElement | null>(null)
const [isFocused, setIsFocused] = useState(false)
const [isFullScreen, setIsFullScreen] = useState(false)
const [isMouseNearBottom, setIsMouseNearBottom] = useState(false)
const [status, setStatus] = useState<'buffering' | 'error' | 'ready'>('ready')
Expand Down Expand Up @@ -140,6 +141,9 @@ const MediaPlayerComponent: FC<Props> = ({ hidden }) => {
if (isFullScreen && event.key === 'Escape') {
event.preventDefault()
event.stopPropagation()
} else if (isFocused && event.key === ' ') {
event.preventDefault()
togglePlay()
}
}

Expand All @@ -152,27 +156,39 @@ const MediaPlayerComponent: FC<Props> = ({ hidden }) => {
}
})

const handlePlayerClick = () => {
togglePlay()
}

return playingNode?.link ? (
<Wrapper ref={wrapperRef} hidden={hidden}>
<Wrapper
ref={wrapperRef}
hidden={hidden}
onBlur={() => setIsFocused(false)}
onFocus={() => setIsFocused(true)}
tabIndex={0}
>
<Cover>
<Avatar size={120} src={playingNode?.image_url || ''} type="clip" />
</Cover>
<ReactPlayer
ref={playerRef}
controls={false}
height={!isFullScreen ? '200px' : window.screen.height}
onBuffer={() => setStatus('buffering')}
onBufferEnd={() => setStatus('ready')}
onError={handleError}
onPause={handlePause}
onPlay={handlePlay}
onProgress={handleProgress}
onReady={handleReady}
playing={isPlaying}
url={playingNode?.link || ''}
volume={volume}
width="100%"
/>
<PlayerWrapper onClick={handlePlayerClick}>
<ReactPlayer
ref={playerRef}
controls={false}
height={!isFullScreen ? '200px' : window.screen.height}
onBuffer={() => setStatus('buffering')}
onBufferEnd={() => setStatus('ready')}
onError={handleError}
onPause={handlePause}
onPlay={handlePlay}
onProgress={handleProgress}
onReady={handleReady}
playing={isPlaying}
url={playingNode?.link || ''}
volume={volume}
width="100%"
/>
</PlayerWrapper>
{status === 'error' ? (
<ErrorWrapper className="error-wrapper">Error happened, please try later</ErrorWrapper>
) : null}
Expand Down Expand Up @@ -206,6 +222,9 @@ const Wrapper = styled(Flex)<Props>`
border-top-left-radius: 16px;
overflow: hidden;
height: ${(props) => (props.hidden ? '0px' : 'auto')};
&:focus {
outline: none;
}
`

const Cover = styled(Flex)`
Expand All @@ -230,4 +249,9 @@ const ErrorWrapper = styled(Flex)`
color: ${colors.primaryRed};
`

const PlayerWrapper = styled.div`
width: 100%;
cursor: pointer;
`

export const MediaPlayer = memo(MediaPlayerComponent)
Loading