本帖最后由 blackpineapple 于 2021-2-17 12:27 编辑
实现了一个播放,暂停合一的按钮,基于文档里的music room代码。
[RenPy] 纯文本查看 复制代码 init python:
# 步骤1,创建一个MusicRoom实例。
mr = MusicRoom(fadeout=1.0)
# Step 2. 添加音乐文件。
mr.add("audio/1.ogg", always_unlocked=True)
mr.add("audio/2.ogg", always_unlocked=True)
mr.add("audio/3.ogg", always_unlocked=True)
class PlayerButton:
def __init__(self, channel='music', icon_path='gui/', mr=mr):
self.channel = channel
self.icon_path = icon_path
self.mr = mr
def get_icon(self):
if not renpy.music.is_playing() and not renpy.music.get_pause():
return self.icon_path + "play_%s.png"
if renpy.music.get_pause(self.channel):
return self.icon_path + "play_%s.png"
return self.icon_path + "pause_%s.png"
def click(self):
if not renpy.music.is_playing() and not renpy.music.get_pause():
self.mr.play()
return
renpy.music.set_pause(not renpy.music.get_pause(self.channel),
channel=self.channel)
play_button = PlayerButton(mr=mr)
label start:
show screen music_room
pause
# Step 3. 创建音乐空间界面。
screen music_room:
timer 1 action [Hide('music_room'), Show('music_room')]
frame:
has vbox
# 每条音轨的播放按钮。
textbutton "Track 1" action mr.Play("audio/1.ogg")
textbutton "Track 2" action mr.Play("audio/2.ogg")
textbutton "Track 3" action mr.Play("audio/3.ogg")
null height 20
# 切换音轨按钮。
textbutton "Next" action mr.Next()
textbutton "Previous" action mr.Previous()
null height 20
# 用户退出音乐空间的按钮。
textbutton "Main Menu" action ShowMenu("main_menu")
python:
duration = str(int(renpy.music.get_duration()))
music_pos = renpy.music.get_pos()
if not music_pos:
music_pos = 0
music_pos = str(int(music_pos))
text duration
text music_pos
bar:
value AudioPositionValue(channel='music', update_interval=0.1)
xalign 0.5
yalign 0.5
xsize 500
xmaximum 500
fixed:
pos(0.5, 0.6)
hbox:
vbox:
text str(renpy.music.get_pause())
imagebutton:
auto play_button.get_icon()
focus_mask True
action Function(play_button.click)
# 音乐空间的音乐播放入口。
on "replace" action mr.Stop()
# 离开时恢复主菜单的音乐。
on "replaced" action Play("music", "track1.ogg")
|