[英]kivy: play sound at start of slide transition and stop when button pressed
i am build a appliaction for my scren to take feed back from people and i want to make annoucement untill a person will come and press start button.我正在为我的屏幕建立一个应用程序以从人们那里获取反馈,我想发布通知,直到有人来按下开始按钮。 the announcement is in mp3 file.公告是 mp3 文件。 i want to play sound when the screen0 appears but want to stop sound when th start button is pressed.how to achieve that please help.我想在 screen0 出现时播放声音,但想在按下开始按钮时停止声音。如何实现,请帮忙。
this is main.py file这是 main.py 文件
from kivy.app import App # Use of fields and methods of Kivy
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class Screen0(Screen):
pass
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class Screen4(Screen):
pass
class Screen5(Screen):
def on_start(self):
Clock.schedule_once(self.change_screen, 0.5)
def change_screen(self, *kwargs):
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
print("bye BYE")
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("style.kv")
class MyApp(App):
def build(self):
return presentation
myApp = MyApp()
myApp.run()
this is kv file这是kv文件
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
transition: SlideTransition()
Screen0:
Screen1:
Screen2:
Screen3:
Screen4:
Screen5:
<Screen0>:
name: "screen0"
FloatLayout:
Label:
text: "PLEASE GIVE US YOUR \n VALUABLE FEEDBACK!"
pos_hint: {"top": 1.2}
font_size: 48
color: 1,1,1,1
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.5, "center_y":.4}
text: "START"
background_color: (0.0, 1.0, 0, 1.0)
font_size: 24
<Screen1>:
name: "screen1"
FloatLayout:
Label:
text: "WHICH IS YOUR FAVORITE SWEET?"
pos_hint: {"top": 1.2}
font_size: 48
color: 1,1,0,1
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.2, "center_y":.4}
text: "HALDIRAMS \n KAJU KATHALI"
background_color: (0.0, 1.0, 1.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.4, "center_y":.4}
text: "HALDIRAMS \n SONPAPDI"
background_color: (1.0, 0.0, 0.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.6, "center_y":.4}
text: "HALDIRAMS \n BADAM HALWA"
background_color: (1.0, 1.0, 0.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.8, "center_y":.4}
text: "HALDIRAMS \n RASGULLA"
background_color: (0.8, 0.3, 0.4, 1.0)
font_size: 18
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: "AFTER HOW MUCH TIME \n YOU GOT THE ORDER?"
pos_hint: {"top": 1.2}
font_size: 48
color: 1,1,0,1
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen3"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.2, "center_y":.4}
text: "Immediatly"
background_color: (0.0, 0.5, 1.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen3"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.4, "center_y":.4}
text: "In 5 to\n 15 Min"
background_color: (1.0, 0.4, 0.8, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen3"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.6, "center_y":.4}
text: "In 15 to\n 30 Min"
background_color: (1.0, 0.8, 0.4, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen3"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.8, "center_y":.4}
text: "45 Min or More"
background_color: (1.0, 0.0, 1.0, 1.0)
font_size: 18
<Screen3>:
name: "screen3"
FloatLayout:
Label:
text: "HOW WAS THE BEHAVIOUR \n OF MANAGER/SERVENT?"
pos_hint: {"top": 1.2}
font_size: 48
color: 1,1,0,1
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen4"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.2, "center_y":.4}
text: "VERY GOOD"
background_color: (0.0, 0.5, 1.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen4"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.4, "center_y":.4}
text: "GOOD"
background_color: (1.0, 0.8, 0.4, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen4"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.6, "center_y":.4}
text: "NOT SO \nGOOD"
background_color: (1.0, 0.4, 0.8, 1.0)
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen4"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.8, "center_y":.4}
text: "VERY BAD"
background_color: (1.0, 1.0, 0.0, 1.0)
<Screen4>:
name: "screen4"
FloatLayout:
Label:
text: "WILL YOU AGAIN SHOP IN \n OUR STORE?"
pos_hint: {"top": 1.2}
font_size: 48
color: 1,1,0,1
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen5"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.2, "center_y":.4}
text: "DEFINETLY"
background_color: (0.5, 1, 1.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen5"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.4, "center_y":.4}
text: "YES"
background_color: (1.0, 0.0, 0.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen5"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.6, "center_y":.4}
text: "NO"
background_color: (1.0, 1.0, 0.0, 1.0)
font_size: 18
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen5"
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.8, "center_y":.4}
text: "CANT SAY"
background_color: (1.0, 0.0, 1.0, 1.0)
font_size: 18
<Screen5>:
name: "screen5"
FloatLayout:
Label:
text: "!!!THANK YOU FOR YOUR\n FEEDBACK+ \n HAVE A NICE DAY!!!"
pos_hint: {"top":1.2}
font_size: 48
color: 1,1,1,1
please share some suggestions请分享一些建议
In your MyApp
add the code to handle the sound:在您的MyApp
中添加处理声音的代码:
class MyApp(App):
def build(self):
self.sound = SoundLoader.load('test.mp3')
self.sound.loop = True
self.sound.play()
return presentation
def stop_sound(self):
self.sound.stop()
Then, in your 'kv' change the Start
Button
rule to:然后,在您的“kv”中将“ Start
Button
”规则更改为:
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
app.stop_sound()
size_hint: 0.2, 0.8/3
pos_hint: {"center_x":0.5, "center_y":.4}
text: "START"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.