Skip to content

Commit

Permalink
Merge pull request #1741 from wgong/pr_audio_video
Browse files Browse the repository at this point in the history
submit audio/video control PR
  • Loading branch information
falkoschindler authored Oct 4, 2023
2 parents 06ee5c4 + 606bb2b commit 503769b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
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))

0 comments on commit 503769b

Please sign in to comment.