Skip to content

Commit

Permalink
feat: add defaultQuality prop (#141)
Browse files Browse the repository at this point in the history
* feat: add defaultQuality prop

* fix: readme

Co-authored-by: zhangyue04 <zhangyue04@zhihu.com>
  • Loading branch information
TaraLoveCats and zhangyue04 authored Sep 23, 2020
1 parent b8109f6 commit 084f2fa
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/griffith/README-zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ render(<Player {...props} />)
| `cover` | `string` | | 视频封面图片 URL |
| `duration` | `number` | | 初始视频时长。在视频元数据载入后使用实际值 |
| `sources` | `sources` | | 视频播放数据。具体见下, |
| `defaultQuality` | `ld \| sd \| hd \| fhd` | | 视频默认播放清晰度 |
| `standalone` | `boolean` | `false` | 是否启用 standalone 模式 |
| `onBeforePlay` | `function` | `void` | 视频播放之前回调函数 |
| `shouldObserveResize` | `boolean` | `false` | 是否监听窗口 resize |
Expand Down
1 change: 1 addition & 0 deletions packages/griffith/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ render(<Player {...props} />)
| `cover` | `string` | | Video cover image |
| `duration` | `number` | | Initial video duration. Use actual values after video metadata is loaded |
| `sources` | `sources` | | Video playback data |
| `defaultQuality` | `ld \| sd \| hd \| fhd` | | Video default quality |
| `standalone` | `boolean` | `false` | Enable standalone mode |
| `onBeforePlay` | `function` | `void` | Callback function before video playback |
| `shouldObserveResize` | `boolean` | `false` | Listen to the window resize |
Expand Down
1 change: 1 addition & 0 deletions packages/griffith/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface PlayerContainerProps {
initialObjectFit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
useMSE?: boolean
locale?: 'en' | 'ja' | 'zh-Hans' | 'zh-Hant'
defaultQuality?: RealQuality[]
}

interface Subscription {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ const PlayerContainer = ({
locale = 'en',
autoplay,
disablePictureInPicture,
defaultQuality,
}) => (
<ObjectFitProvider initialObjectFit={initialObjectFit}>
<PositionProvider shouldObserveResize={shouldObserveResize}>
<MessageProvider id={id} enableCrossWindow={standalone}>
<InternalContext.Consumer>
{({emitEvent, subscribeAction}) => (
<VideoSourceProvider onEvent={emitEvent} sources={sources} id={id}>
<VideoSourceProvider
onEvent={emitEvent}
sources={sources}
id={id}
defaultQuality={defaultQuality}
>
<LocaleContext.Provider value={locale}>
<VideoSourceContext.Consumer>
{({currentSrc}) => (
Expand Down Expand Up @@ -85,6 +91,7 @@ PlayerContainer.propTypes = {
onBeforePlay: PropTypes.func,
initialObjectFit: PropTypes.oneOf(VALID_FIT),
useMSE: PropTypes.bool,
defaultQuality: PropTypes.oneOf(['ld', 'sd', 'hd', 'fhd']),
}

export default PlayerContainer
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default class VideoSourceProvider extends React.Component {
dataKey: null,
}

static getDerivedStateFromProps = ({sources: videoSources, id}, state) => {
static getDerivedStateFromProps = (
{sources: videoSources, id, defaultQuality},
state
) => {
if (!videoSources) return null
const {format, play_url} = Object.values(videoSources)[0]
const {expiration} = parse(play_url)
Expand All @@ -39,7 +42,9 @@ export default class VideoSourceProvider extends React.Component {
if (!isMobile && format === 'm3u8') {
qualities.unshift('auto')
}
const currentQuality = state.currentQuality || qualities[0]

const defaultCurrentQuality = defaultQuality || qualities[0]
const currentQuality = state.currentQuality || defaultCurrentQuality

return {
currentQuality,
Expand Down

0 comments on commit 084f2fa

Please sign in to comment.