Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

submit audio/video control PR #1741

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions nicegui/elements/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@ export default {
compute_src() {
this.computed_src = (this.src.startsWith("/") ? window.path_prefix : "") + this.src;
},
seek(seconds) {
this.$el.currentTime = seconds;
},
play() {
this.$el.play();
},
pause() {
this.$el.pause();
},
},
};
15 changes: 15 additions & 0 deletions nicegui/elements/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ def __init__(self, src: Union[str, Path], *,
if type:
url = 'https://github.com/zauberzeug/nicegui/pull/624'
warnings.warn(DeprecationWarning(f'The type parameter for ui.audio is deprecated and ineffective ({url}).'))

def seek(self, seconds: float) -> None:
"""Seek to a specific position in the audio.

:param seconds: the position in seconds
"""
self.run_method('seek', seconds)

def play(self) -> None:
"""Play audio."""
self.run_method('play')

def pause(self) -> None:
"""Pause audio."""
self.run_method('pause')
6 changes: 6 additions & 0 deletions nicegui/elements/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ export default {
seek(seconds) {
this.$el.currentTime = seconds;
},
play() {
this.$el.play();
},
pause() {
this.$el.pause();
},
},
};
8 changes: 8 additions & 0 deletions nicegui/elements/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ def seek(self, seconds: float) -> None:
:param seconds: the position in seconds
"""
self.run_method('seek', seconds)

def play(self) -> None:
"""Play video."""
self.run_method('play')

def pause(self) -> None:
"""Pause video."""
self.run_method('pause')
13 changes: 13 additions & 0 deletions website/more_documentation/audio_documentation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
from nicegui import ui

from ..documentation_tools import text_demo


def main_demo() -> None:
a = ui.audio('https://cdn.pixabay.com/download/audio/2022/02/22/audio_d1718ab41b.mp3')
a.on('ended', lambda _: ui.notify('Audio playback completed'))

ui.button(on_click=lambda: a.props('muted'), icon='volume_off').props('outline')
ui.button(on_click=lambda: a.props(remove='muted'), icon='volume_up').props('outline')


def more() -> None:
@text_demo('Control the audio element', '''
This demo shows how to play, pause and seek programmatically.
''')
def control_demo() -> None:
a = ui.audio('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3')
ui.button('Play', on_click=a.play)
ui.button('Pause', on_click=a.pause)
ui.button('Jump to 0:30', on_click=lambda: a.seek(30))
10 changes: 6 additions & 4 deletions website/more_documentation/video_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def main_demo() -> None:


def more() -> None:
@text_demo('Video start position', '''
This demo shows how to set the start position of a video.
@text_demo('Control the video element', '''
This demo shows how to play, pause and seek programmatically.
''')
def start_position_demo() -> None:
def control_demo() -> None:
v = ui.video('https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4')
v.on('loadedmetadata', lambda: v.seek(5))
ui.button('Play', on_click=v.play)
ui.button('Pause', on_click=v.pause)
ui.button('Jump to 0:05', on_click=lambda: v.seek(5))