diff --git a/.gitignore b/.gitignore index abe98a0..2cc3697 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ venv/ *.egg-info *.log .DS_Store -.idea \ No newline at end of file +.idea +*.ipynb_checkpoints \ No newline at end of file diff --git a/src/components/PlayerComponent.react.js b/src/components/PlayerComponent.react.js index daba60f..7176256 100644 --- a/src/components/PlayerComponent.react.js +++ b/src/components/PlayerComponent.react.js @@ -3,11 +3,9 @@ import ReactPlayer from 'react-player'; import PropTypes from 'prop-types'; /** - * ExampleComponent is an example component. - * It takes a property, `label`, and - * displays it. - * It renders an input with the property `value` - * which is editable by the user. + * A Dash component for playing a variety of URLs, including file paths, + * YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia, Mixcloud, + * and DailyMotion. */ export default class PlayerComponent extends Component { constructor(props) { @@ -16,6 +14,7 @@ export default class PlayerComponent extends Component { this.updateCurrentTime = this.updateCurrentTime.bind(this); this.updateInstanceMethods = this.updateInstanceMethods.bind(this); + this.setSeekTo = this.setSeekTo.bind(this); setInterval(this.updateCurrentTime, 40); // 25 FPS setInterval(this.updateInstanceMethods, 500); } @@ -41,6 +40,27 @@ export default class PlayerComponent extends Component { } } + /** + * Applies the seekTo() method to the player if the seekTo prop + * contains any value, then set seekTo to null. If seekTo was already + * null, it doesn't do anything. + */ + setSeekTo(){ + const { + seekTo, + setProps + } = this.props; + + if (seekTo !== null && setProps !== null){ + this.refs.player.seekTo(seekTo); + setProps({seekTo: null}); + } + } + + componentDidUpdate(){ + this.setSeekTo(); + } + render() { const { url, @@ -170,7 +190,12 @@ PlayerComponent.propTypes = { /** * Returns the duration (in seconds) of the currently playing media */ - duration: PropTypes.number + duration: PropTypes.number, + + /** + * Seek to the given number of seconds, or fraction if amount is between 0 and 1 + */ + seekTo: PropTypes.number }; PlayerComponent.defaultProps = { @@ -184,5 +209,6 @@ PlayerComponent.defaultProps = { height: '360px', style:{}, progressInterval: 1000, - playsinline: false + playsinline: false, + seekTo: null }; \ No newline at end of file diff --git a/usage-advanced.py b/usage-advanced.py index 5e2f616..13ea80c 100644 --- a/usage-advanced.py +++ b/usage-advanced.py @@ -13,7 +13,7 @@ app.scripts.config.serve_locally = True app.layout = html.Div([ html.Div( - style={'width': '40%', 'float': 'left', 'margin-right': '5%'}, + style={'width': '40%', 'float': 'left', 'margin': '0% 5% 0% 5%'}, children=[ dash_player.PlayerComponent( id='video-player', @@ -73,7 +73,18 @@ app.layout = html.Div([ updatemode='drag', marks={i: str(i) + 'x' for i in [0, 0.25, 0.5, 0.75, 1, 2, 3, 4]}, value=1 - ) + ), + + html.P("Seek To:", style={'margin-top': '30px'}), + dcc.Slider( + id='slider-seek-to', + min=0, + max=1, + step=None, + updatemode='drag', + marks={i: str(i*100) + '%' for i in [0, 0.25, 0.5, 0.75, 1]}, + value=0 + ), ]), ]) @@ -135,5 +146,10 @@ def update_methods(secondsLoaded, duration): return 'Second Loaded: {}, Duration: {}'.format(secondsLoaded, duration) +@app.callback(Output('video-player', 'seekTo'), + [Input('slider-seek-to', 'value')]) +def set_seekTo(value): + return value + if __name__ == '__main__': app.run_server(debug=True) diff --git a/usage-methods.py b/usage-methods.py index 650c9fc..f63992c 100644 --- a/usage-methods.py +++ b/usage-methods.py @@ -16,6 +16,8 @@ app.layout = html.Div([ controls=True ), + html.Button('Set seekTo to 10', id='button-seek-to'), + html.Div(id='div-current-time', style={'margin-bottom': '20px'}), html.Div(id='div-method-output') @@ -35,5 +37,11 @@ def update_methods(secondsLoaded, duration): return 'Second Loaded: {}, Duration: {}'.format(secondsLoaded, duration) +@app.callback(Output('video-player', 'seekTo'), + [Input('button-seek-to', 'n_clicks')]) +def set_seekTo(n_clicks): + return 10 + + if __name__ == '__main__': app.run_server(debug=True) diff --git a/usage.py b/usage.py index cc011b6..fa551f4 100644 --- a/usage.py +++ b/usage.py @@ -22,10 +22,11 @@ app.layout = html.Div([ 'playing', 'loop', 'controls', - 'muted' + 'muted', + 'seekTo' ]], values=['controls'] - ) + ), ]) @@ -53,5 +54,12 @@ def update_prop_muted(values): return 'muted' in values +@app.callback(Output('video-player', 'seekTo'), + [Input('radio-bool-props', 'values')]) +def update_prop_seekTo(values): + if 'seekTo' in values: + return 5 + + if __name__ == '__main__': app.run_server(debug=True)