切换音乐 On/Off 绝望
Toggle music On/Off Kivy
这是我的 .py 文件的一部分。每次切换按钮时,我都希望 on_state 到 运行,但它不起作用。我将打印语句放入测试每个部分,每次切换按钮时它都会打印 "ONSTATE!!!" 。但是 'down' 或 'normal' 状态都不会打印。
class MusicScreen(Screen):
enter = ObjectProperty(None)
text_input = ObjectProperty(None)
stop = ObjectProperty(None)
musicbutton = ToggleButton()
class MainApp(App):
def build(self):
return presentation
def on_state(self, state, filename):
print("ONSTATE!!!")
sound = SoundLoader.load('/users/nolandonley/desktop/MP3/mus/' + filename + '.m4a')
if state == "down":
print("DOWN")
sound.volume = .5
sound.play()
if state == "normal":
print("normal")
sound.stop()
下面是我的项目 .kv 文件。
<MusicScreen>:
text_input: text_input
id: "music"
name: "music"
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
rootpath: "/Users/nolandonley/desktop/MP3/mus"
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 50
multiline: False
ToggleButton:
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By File"
ToggleButton:
id: musicbutton
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By Title"
on_state: app.on_state(self, text_input.text)
停止播放音乐
使用 ObjectProperty 和 sound.unload() 方法停止播放音乐。
main.py
class MainApp(App):
sound = ObjectProperty(None, allownone=True)
def build(self):
return ScreenManagement()
def on_state(self, state, filename):
print("ONSTATE!!!")
print("\tstate=", state)
if self.sound is None:
self.sound = SoundLoader.load(filename)
# stop the sound if it's currently playing
if self.sound.status != 'stop':
self.sound.stop()
if state == "down":
self.sound.volume = .5
self.sound.play()
else: # if state == "normal":
if self.sound:
self.sound.stop()
self.sound.unload()
self.sound = None
传递切换按钮的状态
它不起作用,因为您正在传递一个对象,即切换按钮的一个实例。
替换
on_state: app.on_state(self, text_input.text)
与
on_state: app.on_state(self.state, text_input.text)
备注
一个[Toggle Button][1]
只有两个状态,即正常和向下。因此,您不需要两个 if
语句。通过以下方式提高 Kivy 应用程序的性能:
正在替换
if state == "down":
print("DOWN")
sound.volume = .5
sound.play()
if state == "normal":
print("normal")
sound.stop()
与:
print(state)
if state == "down":
sound.volume = .5
sound.play()
else: # if state == "normal":
sound.stop()
这是我的 .py 文件的一部分。每次切换按钮时,我都希望 on_state 到 运行,但它不起作用。我将打印语句放入测试每个部分,每次切换按钮时它都会打印 "ONSTATE!!!" 。但是 'down' 或 'normal' 状态都不会打印。
class MusicScreen(Screen):
enter = ObjectProperty(None)
text_input = ObjectProperty(None)
stop = ObjectProperty(None)
musicbutton = ToggleButton()
class MainApp(App):
def build(self):
return presentation
def on_state(self, state, filename):
print("ONSTATE!!!")
sound = SoundLoader.load('/users/nolandonley/desktop/MP3/mus/' + filename + '.m4a')
if state == "down":
print("DOWN")
sound.volume = .5
sound.play()
if state == "normal":
print("normal")
sound.stop()
下面是我的项目 .kv 文件。
<MusicScreen>:
text_input: text_input
id: "music"
name: "music"
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
rootpath: "/Users/nolandonley/desktop/MP3/mus"
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 50
multiline: False
ToggleButton:
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By File"
ToggleButton:
id: musicbutton
#pos: 44, 30
size_hint: 1, .2
text: "Play/Stop By Title"
on_state: app.on_state(self, text_input.text)
停止播放音乐
使用 ObjectProperty 和 sound.unload() 方法停止播放音乐。
main.py
class MainApp(App):
sound = ObjectProperty(None, allownone=True)
def build(self):
return ScreenManagement()
def on_state(self, state, filename):
print("ONSTATE!!!")
print("\tstate=", state)
if self.sound is None:
self.sound = SoundLoader.load(filename)
# stop the sound if it's currently playing
if self.sound.status != 'stop':
self.sound.stop()
if state == "down":
self.sound.volume = .5
self.sound.play()
else: # if state == "normal":
if self.sound:
self.sound.stop()
self.sound.unload()
self.sound = None
传递切换按钮的状态
它不起作用,因为您正在传递一个对象,即切换按钮的一个实例。
替换
on_state: app.on_state(self, text_input.text)
与
on_state: app.on_state(self.state, text_input.text)
备注
一个[Toggle Button][1]
只有两个状态,即正常和向下。因此,您不需要两个 if
语句。通过以下方式提高 Kivy 应用程序的性能:
正在替换
if state == "down":
print("DOWN")
sound.volume = .5
sound.play()
if state == "normal":
print("normal")
sound.stop()
与:
print(state)
if state == "down":
sound.volume = .5
sound.play()
else: # if state == "normal":
sound.stop()