From 079e1d5ea62528c0436d7225df44ef9c88254c73 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Sat, 23 Aug 2014 17:06:37 +0200 Subject: [PATCH] example: add slider volume for audio examples. Refs #1869 --- examples/audio/audio.kv | 15 ++++++++++++--- examples/audio/main.py | 13 ++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/examples/audio/audio.kv b/examples/audio/audio.kv index 6a76e9894..92fa0b9b8 100644 --- a/examples/audio/audio.kv +++ b/examples/audio/audio.kv @@ -32,12 +32,21 @@ font_size: 32 size_hint_y: None - StackLayout: - id: sl + BoxLayout: + Slider: + min: 0.0 + max: 1.0 + value: 1.0 + on_value: app.set_volume(self.value) + orientation: "vertical" + size_hint_x: None + width: "48dp" + + StackLayout: + id: sl Button: text: 'Stop and release all audio' size_hint_y: None height: '50sp' on_press: app.release_audio() - diff --git a/examples/audio/main.py b/examples/audio/main.py index 3b63d571d..a203d0332 100644 --- a/examples/audio/main.py +++ b/examples/audio/main.py @@ -14,7 +14,7 @@ from kivy.app import App from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout from kivy.core.audio import SoundLoader -from kivy.properties import StringProperty, ObjectProperty +from kivy.properties import StringProperty, ObjectProperty, NumericProperty from glob import glob from os.path import dirname, join, basename @@ -23,6 +23,7 @@ class AudioButton(Button): filename = StringProperty(None) sound = ObjectProperty(None, allownone=True) + volume = NumericProperty(1.0) def on_press(self): if self.sound is None: @@ -30,6 +31,7 @@ class AudioButton(Button): # stop the sound if it's currently playing if self.sound.status != 'stop': self.sound.stop() + self.sound.volume = self.volume self.sound.play() def release_audio(self): @@ -38,6 +40,11 @@ class AudioButton(Button): self.sound.unload() self.sound = None + def set_volume(self, volume): + self.volume = volume + if self.sound: + self.sound.volume = volume + class AudioBackground(BoxLayout): pass @@ -61,5 +68,9 @@ class AudioApp(App): for audiobutton in self.root.ids.sl.children: audiobutton.release_audio() + def set_volume(self, value): + for audiobutton in self.root.ids.sl.children: + audiobutton.set_volume(value) + if __name__ == '__main__': AudioApp().run()