I have a sample script here which play sound only once after it is loaded.
import dash
import dash_html_components as html
import base64
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
# Encode the local sound file.
sound_filename = 'path_to_your_file.mp3' # replace with your own .mp3 file
encoded_sound = base64.b64encode(open(sound_filename, 'rb').read())
app.layout = html.Div(children=[
html.H1(children='Demo for Audio with Dash'),
html.Div(children='''
Click the button to play your local .mp3 sounds.
'''),
html.Button(id="button1", children="Click me for sound"),
html.Div(id="placeholder", style={"display": "none"})])
@app.callback(Output("placeholder", "children"),
[Input("button1", "n_clicks")],
)
def play(n_clicks):
if n_clicks is None:
n_clicks = 0
if n_clicks != 0:
return html.Audio(src='data:audio/mpeg;base64,{}'.format(encoded_sound.decode()),
controls=False,
autoPlay=True,
)
if __name__ == '__main__':
app.run_server(debug=True)
what I want is to play sound each time the button is clicked. Let me know if this is possible. Thank you!
2 posts - 1 participant