马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 怀念丶落樱 于 2022-4-10 23:11 编辑
这是我的代码(空间内找的)我想添加一个显示正在播放音乐名字的功能,我在文档内找到了
renpy.music.get_playing(channel='music') 若入参channel上有音频正在播放,返回文件名。否则返回None。
当我运行程序时,音乐开始播放,返回值显示None,它没有返回名字。
我将代码添加到63行与106行。
[RenPy] 纯文本查看 复制代码
define aaa = ""
init python:
def get_audio_duration():
duration = renpy.music.get_duration()
return convert_format(int(duration))
def get_audio_position():
music_pos = renpy.music.get_pos()
if music_pos:
return convert_format(int(music_pos))
return "0"
def convert_format(second):
minute = second // 60
second = second % 60
result = ""
if minute:
result = str(minute) + ":"
if second < 10:
result += '0'
elif second>9:
result +='0:'
else:
result +='0:0'
result += str(second)
return result
#第二部分
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)
aaa = renpy.music.get_playing(channel='music')
label start:
call screen music_room
pause
# Step 3. 创建音乐空间界面。
screen music_room:
#timer 1 action [Hide('music_room'), Show('music_room')]
timer 0.1:
action [SetVariable('duration',get_audio_duration()),SetVariable('music_pos',get_audio_position())]
repeat True
frame:
style "music_room_frame"
style_prefix "navigation"
has vbox
#pos(0.3, 0.5)
# 每条音轨的播放按钮。
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
# 切换音轨按钮。
null height 20
# 用户退出音乐空间的按钮。
textbutton "退出" action [Hide("music_room"),Stop('music', fadeout=2.0),SetVariable("jiemianxianshi",int(0)),Play("music", "track1.ogg")]
python:
duration = get_audio_duration()
music_pos = get_audio_position()
fixed:
hbox:
pos(0.3, 0.8)
spacing 20
text music_pos
text "/"
text duration
text "[aaa]"
bar:
value AudioPositionValue(channel='music', update_interval=0.1)
xpos 0
ypos 0
xsize 100
xmaximum 100
#上一首音乐
imagebutton:
xalign 0.5 yalign 0.5
idle "music_room/backward.png"
hover "music_room/backwardHover.png"
action mr.Previous()
#暂停按钮
imagebutton:
auto play_button.get_icon()
focus_mask True
action Function(play_button.click)
#下一首音乐
imagebutton:
xalign 0.5 yalign 0.5
idle "music_room/forward.png"
hover "music_room/forwardHover.png"
action mr.Next()
#音量组件
if config.has_music:
label _("音乐音量")
hbox:
bar value Preference("music volume") xsize 100
style music_room_frame:
yfill True
background "gui/overlay/main_menu.png"
|