How to return a music name?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
luoying
Newbie
Posts: 7
Joined: Wed Apr 06, 2022 8:31 pm
Contact:

How to return a music name?

#1 Post by luoying » Sun Apr 10, 2022 4:12 am

This is my code. I want to add a function to display the name of the music being played,I found it renpy.music.get_playing(channel='music') 。
When I run the program, the music starts playing, and the return value displays none, which does not return the name of the music.
I added the code to lines 63 and 106
I'm a Chinese player. I'm not good at English. If it bothers you, I'm sorry.

Code: Select all

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"

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to return a music name?

#2 Post by Ocelot » Sun Apr 10, 2022 4:30 am

You are setting value of aaa once, during init phase, when the game is starting, where no music will play.
You need to constantly refresh it, as you did with music_pos and duration variables.
< < insert Rick Cook quote here > >

luoying
Newbie
Posts: 7
Joined: Wed Apr 06, 2022 8:31 pm
Contact:

Re: How to return a music name?

#3 Post by luoying » Sun Apr 10, 2022 11:09 am

Oh, yes, thank you for your help, thank you!

Post Reply

Who is online

Users browsing this forum: Google [Bot]