Skip to content

Commit

Permalink
feat: add shortcut keys for playback rate
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Jan 27, 2022
1 parent d76c811 commit 8c319dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
26 changes: 25 additions & 1 deletion packages/griffith/src/components/Controller.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useRef, useState} from 'react'
import React, {useContext, useEffect, useRef, useState} from 'react'
import {css} from 'aphrodite/no-important'
import clamp from 'lodash/clamp'
import * as displayIcons from './icons/display/index'
Expand All @@ -17,6 +17,7 @@ import PageFullScreenButtonItem from './items/PageFullScreenButtonItem'
import useHandler from '../hooks/useHandler'
import useBoolean from '../hooks/useBoolean'
import {useActionToastDispatch} from './ActionToast'
import VideoSourceContext from '../contexts/VideoSourceContext'

type ControllerProps = {
standalone?: boolean
Expand Down Expand Up @@ -105,11 +106,26 @@ function Controller(props: ControllerProps) {
onSeek,
onVolumeChange,
} = props
const {playbackRates, currentPlaybackRate, setCurrentPlaybackRate} =
useContext(VideoSourceContext)
const actionToastDispatch = useActionToastDispatch()
const [isVolumeHovered, isVolumeHoveredSwitch] = useBoolean()
const [slideTime, setSlideTime] = useState<number>()
const prevVolumeRef = useRef(1)

const rotatePlaybackRate = (dir: 'next' | 'prev') => {
const index = playbackRates?.findIndex(
(x) => x.value === currentPlaybackRate.value
)
if (index >= 0) {
const next = playbackRates[index + (dir === 'next' ? 1 : -1)]
if (next) {
actionToastDispatch({icon: displayIcons.play, label: next.text})
setCurrentPlaybackRate(next)
}
}
}

const handleDragMove = useHandler((slideTime: number) => {
setSlideTime(clamp(slideTime, 0, duration))
})
Expand Down Expand Up @@ -219,6 +235,14 @@ function Controller(props: ControllerProps) {
handleVolumeChange(volume - 0.05, true)
break

case '<':
rotatePlaybackRate('prev')
break

case '>':
rotatePlaybackRate('next')
break

default:
handled = false
break
Expand Down
1 change: 1 addition & 0 deletions packages/griffith/src/components/Player.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default StyleSheet.create({
},

actionLabel: {
marginTop: '.5em',
padding: '.3em .5em',
textAlign: 'center',
color: '#fff',
Expand Down
4 changes: 2 additions & 2 deletions packages/griffith/src/contexts/VideoSourceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export type VideoSourceContextValue = {
sources: PlaySource[]
// 视频质量
qualities: Quality[]
setCurrentQuality(x: Quality): void
setCurrentQuality: (x: Quality) => void
currentQuality: Quality
// 播放速度
playbackRates: PlaybackRate[]
setCurrentPlaybackRate(x: PlaybackRate): void
setCurrentPlaybackRate: (x: PlaybackRate) => void
currentPlaybackRate: PlaybackRate
}

Expand Down

0 comments on commit 8c319dc

Please sign in to comment.