example: add slider volume for audio examples. Refs #1869

This commit is contained in:
Mathieu Virbel 2014-08-23 17:06:37 +02:00
parent 3b7d31a8b4
commit 079e1d5ea6
2 changed files with 24 additions and 4 deletions

View File

@ -32,6 +32,16 @@
font_size: 32 font_size: 32
size_hint_y: None size_hint_y: None
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: StackLayout:
id: sl id: sl
@ -40,4 +50,3 @@
size_hint_y: None size_hint_y: None
height: '50sp' height: '50sp'
on_press: app.release_audio() on_press: app.release_audio()

View File

@ -14,7 +14,7 @@ from kivy.app import App
from kivy.uix.button import Button from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.core.audio import SoundLoader from kivy.core.audio import SoundLoader
from kivy.properties import StringProperty, ObjectProperty from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from glob import glob from glob import glob
from os.path import dirname, join, basename from os.path import dirname, join, basename
@ -23,6 +23,7 @@ class AudioButton(Button):
filename = StringProperty(None) filename = StringProperty(None)
sound = ObjectProperty(None, allownone=True) sound = ObjectProperty(None, allownone=True)
volume = NumericProperty(1.0)
def on_press(self): def on_press(self):
if self.sound is None: if self.sound is None:
@ -30,6 +31,7 @@ class AudioButton(Button):
# stop the sound if it's currently playing # stop the sound if it's currently playing
if self.sound.status != 'stop': if self.sound.status != 'stop':
self.sound.stop() self.sound.stop()
self.sound.volume = self.volume
self.sound.play() self.sound.play()
def release_audio(self): def release_audio(self):
@ -38,6 +40,11 @@ class AudioButton(Button):
self.sound.unload() self.sound.unload()
self.sound = None self.sound = None
def set_volume(self, volume):
self.volume = volume
if self.sound:
self.sound.volume = volume
class AudioBackground(BoxLayout): class AudioBackground(BoxLayout):
pass pass
@ -61,5 +68,9 @@ class AudioApp(App):
for audiobutton in self.root.ids.sl.children: for audiobutton in self.root.ids.sl.children:
audiobutton.release_audio() audiobutton.release_audio()
def set_volume(self, value):
for audiobutton in self.root.ids.sl.children:
audiobutton.set_volume(value)
if __name__ == '__main__': if __name__ == '__main__':
AudioApp().run() AudioApp().run()