【发布时间】:2021-04-16 16:16:39
【问题描述】:
我正在构建一个 Dash 应用程序,它将播放视频并由播放/暂停按钮控制。在视频上方,我有一个滑块,允许用户跳到视频中的任何部分(10 秒长)。我希望能够让滑块在视频播放时移动。
这是我迄今为止尝试过的:
import os
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_player as player
from flask import Flask, Response
from dash.dependencies import Input, Output, State
server = Flask(__name__)
app = dash.Dash(__name__, server=server)
app.layout = html.Div(
children=[
html.Div(
children=[
html.Div([
html.Button('Play/Pause', id='play-button', n_clicks=0),
],style={
'width':'10%',
'display':'inline-block'
}),
html.Div([
dcc.Slider(id='slider',
min=0,
max=10,
step=1,
value=0,
marks={0: {'label': '0s', 'style': {'color': 'black'}},
2: {'label': '2s', 'style': {'color': 'black'}},
4: {'label': '4s', 'style': {'color': 'black'}},
6: {'label': '6s', 'style': {'color': 'black'}},
8: {'label': '8s', 'style': {'color': 'black'}},
10: {'label': '10s', 'style': {'color': 'black'}}
}
),
],style={
'width':'90%',
'display':'inline-block'
})
]
),
html.Div(
children=[
player.DashPlayer(
id='video-player',
url='https://www.w3schools.com/html/mov_bbb.mp4',
controls=False,
width='100%'
)
]
)
]
)
#Use slider to skip to or "seekTo" different spots in video
@app.callback(Output('video-player', 'seekTo'),
Input('slider', 'value'))
def set_seekTo(value):
return value
#Press button to play/pause video 1
@app.callback(
Output("video-player", "playing"),
Output("slider","value"),
Input("play-button", "n_clicks"),
State("video-player", "playing"),
State('slider','value'),
State("video-player","currentTime")
)
def play_video(n_clicks, playing, value, currentTime):
value = currentTime
if n_clicks:
return not playing, value
return playing, value
if __name__ == "__main__":
app.run_server(debug=True, port=8050)
播放/暂停按钮工作正常,但将滑块同步到视频的 currentTime 的功能被忽略了。没有错误信息。我在这里想念什么?任何帮助将不胜感激!
【问题讨论】:
-
这是因为您没有 currentTime 或其他时间间隔作为回调的输入。我能够有时间与滑块同步,但我无法让它与
seekTo结合使用 -
您能分享一下您是如何有时间与滑块同步的吗?
标签: python video slider plotly-dash plotly-python