Bladeren bron

Merge pull request #1741 from wgong/pr_audio_video

submit audio/video control PR
Falko Schindler 1 jaar geleden
bovenliggende
commit
503769bd15

+ 9 - 0
nicegui/elements/audio.js

@@ -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 - 0
nicegui/elements/audio.py

@@ -40,3 +40,18 @@ class Audio(Element, component='audio.js'):
         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 - 0
nicegui/elements/video.js

@@ -24,5 +24,11 @@ export default {
     seek(seconds) {
       this.$el.currentTime = seconds;
     },
+    play() {
+      this.$el.play();
+    },
+    pause() {
+      this.$el.pause();
+    },
   },
 };

+ 8 - 0
nicegui/elements/video.py

@@ -47,3 +47,11 @@ class Video(Element, component='video.js'):
         :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 - 0
website/more_documentation/audio_documentation.py

@@ -1,5 +1,7 @@
 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')
@@ -7,3 +9,14 @@ def main_demo() -> None:
 
     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))

+ 6 - 4
website/more_documentation/video_documentation.py

@@ -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))