Music Player UI screen help

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
User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Music Player UI screen help

#1 Post by lsf22 »

Renpy version: 8.0.3

I've been trying to get a UI screen for a music player going and I've encountered a few problems I can't figure out with it.

1. How can I get it to show the track number or name in the hbox text (doesn't have to be a hbox either)

2. What am I getting wrong with the Next function? (For next track/bgm)

I have attached the project folder as well

code:

Code: Select all

# Music player script

# defined bgm/tracks
define audio.bgm1 = "/audio/bgm_1.mp3"
define audio.bgm2 = "/audio/bgm_2.mp3"
define audio.bgm3 = "/audio/bgm_3.mp3"
define audio.bgm4 = "/audio/bgm_4.mp3"
define audio.bgm5 = "/audio/bgm_5.mp3"
define audio.bgm6 = "/audio/bgm_6.mp3"
define audio.bgm7 = "/audio/bgm_7.mp3"

# playlists
define playlist0 = [audio.bgm1, audio.bgm2, audio.bgm3, audio.bgm4, audio.bgm5, audio.bgm6, audio.bgm7]
define playlist1 = [audio.bgm1, audio.bgm2, audio.bgm3, audio.bgm4]
define playlist2 = [audio.bgm5, audio.bgm6, audio.bgm7]

# images defined for imagemap
image imagemap_hover_img = "/images/ui_icons/Image_map_icon_music_1_hover.png"
image imagemap_idle_img = "/images/ui_icons/Image_map_icon_music_1_idle.png"

init python:

    def start_music_shuffle(playlist ):
        renpy.random.shuffle(playlist)
        renpy.music.play(playlist, loop=True)

    def start_music_play(playlist ):
        renpy.music.play(playlist, loop=True)

screen UI_Icon_MusicPlayer():

    imagemap:
        xpos 0.005
        ypos 0.005
        ground "/images/ui_icons/Image_map_icon_music_1_idle.png"
        idle "imagemap_idle_img"
        hover "imagemap_hover_img"
        hotspot (297, 322, 91, 102) action Function(start_music_play, playlist0)
        hotspot (183, 324, 102, 99) action Function(start_music_shuffle, playlist0) #shuffle
        hotspot (405, 328, 89, 97) action Stop(channel='music')
        #hotspot (528, 322, 131, 106) action Next()  ## What am I doing wrong with this?
        #hotspot (38, 320, 135, 110) action Previous() ## What am I doing wrong with this?

    hbox:
        xpos 0.02
        ypos 0.08
        text "[playlist0]" ## currently shows list instead of just the currently playing track
Attachments
test music player ui.zip
Project folder
(30.74 MiB) Downloaded 25 times
Failed to show track number or name
Failed to show track number or name

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#2 Post by enaielei »

Have you tried looking into the built-in MusicRoom before making your own?

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#3 Post by lsf22 »

I did before, I wasn't sure about it being meant for that use.

I did go ahead and revised it a bit to be similar to the Music Room to work.

I'm still not getting a proper track name back. I've been seeing with the current code that the it is not accurate with the file names and that it does not show up with other functions. Sometimes it doesn't refresh fast enough to update to the new track name when a new track is played.

code:

Code: Select all

# Music player script

define track_name = ""

# images defined for imagemap
image imagemap_hover_img = "/images/ui_icons/Image_map_icon_music_1_hover.png"
image imagemap_idle_img = "/images/ui_icons/Image_map_icon_music_1_idle.png"

init python:

    # Step 1. Create a MusicRoom instance.
    mr = MusicRoom(channel='music',fadeout=1.0)

    # Step 2. Add music files.
    mr.add("audio/bgm_1.mp3", always_unlocked=True)
    mr.add("audio/bgm_2.mp3", always_unlocked=True)
    mr.add("audio/bgm_3.mp3", always_unlocked=True)
    mr.add("audio/bgm_4.mp3", always_unlocked=True)
    mr.add("audio/bgm_5.mp3", always_unlocked=True)
    mr.add("audio/bgm_6.mp3", always_unlocked=True)
    mr.add("audio/bgm_7.mp3", always_unlocked=True)


screen UI_Icon_MusicPlayer():

    imagemap:
        xpos 0.01
        ypos 0.01
        ground "/images/ui_icons/Image_map_icon_music_1_idle.png"
        idle "imagemap_idle_img"
        hover "imagemap_hover_img"
        hotspot (297, 322, 91, 102) action mr.Play()
        hotspot (183, 324, 102, 99) action mr.RandomPlay()
        hotspot (405, 328, 89, 97) action mr.Stop()
        hotspot (528, 322, 131, 106) action mr.Next()
        hotspot (38, 320, 135, 110) action mr.Previous()

    hbox:
        xpos 0.05
        ypos 0.09
        text "[track_name]" color 'f0f4ff'

    python:
        track_name = renpy.music.get_playing(channel='music')
Attachments
renpy music player ui test 1.mkv.zip
Video test of it not responding well on Next/Previous
(286.31 KiB) Downloaded 25 times
Test Music Player UI 2.zip
Project folder with revised code
(30.72 MiB) Downloaded 32 times
When nothing is played, Play is not initiated from the image map hotspot
When nothing is played, Play is not initiated from the image map hotspot
Playing 1st track.  Get name is file location instead of actual track name
Playing 1st track. Get name is file location instead of actual track name

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#4 Post by enaielei »

For the track name, you can do this.

Code: Select all

init python:
    def get_playing(music_room):
        return renpy.music.get_playing(music_room.channel) or ""

screen UI_Icon_MusicPlayer():
    $ track_name = get_playing(mr)
    text track_name
Additionally, if you want a proper name and not just the filename you can create a mapping and use that.

Code: Select all

define audios = {
    "audio/test1.ogg": "Test 1 Track",
}

screen UI_Icon_MusicPlayer():
    $ track_name = audios.get(get_playing(mr), "")
    text track_name

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#5 Post by lsf22 »

The only thing left for me solve for it at the moment is the refreshing of the track names. Especially when using the Next and Previous actions.

For the code I only omitted the text code on line 31 because it kept showing underneath the music player image map.

You can see in the attached video of the track names not refreshing.
Attachments
Music Player UI testing 2.mkv.zip
Video file MKV format
(798.2 KiB) Downloaded 28 times

sandpie
Newbie
Posts: 18
Joined: Tue Apr 25, 2023 1:43 pm
Contact:

Re: Music Player UI screen help

#6 Post by sandpie »

I guess that happens because screens are not contiously updated, then you will have to forcefully update your screen using
renpy.restart_interaction()
unfortunately I can't tell you where you should put this line: I would try to call it anytime you change your track but I am still supernoob in renpy and I can't do anything here yet.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#7 Post by enaielei »

You can try this.
Instead of using mr.Next() use Function(mr.Next) instead.

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#8 Post by lsf22 »

If i'm trying to use the function mr.RandomPlay, what is the equivalent of Function(mr.RandomPlay) for a One-line python statement?

Ive tried the following but it does not work. This code is what I'm trying to run in another label for a shop.

Code: Select all

$ Function(mr.RandomPlay())
neither does this work

Code: Select all

$ action.mr.RandomPlay()

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#9 Post by enaielei »

renpy.run(mr.RandomPlay()) or mr.RandomPlay()()

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#10 Post by lsf22 »

enaielei wrote: Tue May 02, 2023 5:49 pm renpy.run(mr.RandomPlay()) or mr.RandomPlay()()
Thanks, that worked

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#11 Post by lsf22 »

If i have music added to the Music Room, how does one set it to unlock somewhere in a label? What are the ways I can get a always_unlocked==False song to unlock, I've been struggling with this for the past 20 something minutes.

Music_Room.rpy script

Code: Select all

mr.add("/audio/bgms/bgm_6.ogg", always_unlocked=False)

I've tried the following but it did not work, instead it just played the first song

Code: Select all

"Move forward":
            $ mr.Play("/audio/bgms/bgm_6.ogg")()
            if scn_6 ==False:
                jump Scene_6
Last edited by lsf22 on Thu May 18, 2023 4:57 pm, edited 1 time in total.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#12 Post by enaielei »

always_unlocked states that when it's set to False the music will be unlocked in the music room if it has been played at least one time in the game.
In short, don't use the Play action of the MusicRoom. Just play it normally like play music "/audio/bgms/bgm_6.ogg" and it will automatically get unlocked in the music room.

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#13 Post by lsf22 »

Thank you. I found it odd that using Play Music bgm_6 did not work even though it seems like the same thing without being more specific.

Code: Select all

play music bgm_6

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Music Player UI screen help

#14 Post by enaielei »

In your previous codes, you were defining it as audio.bgm6 and not as audio.bgm_6.

User avatar
lsf22
Regular
Posts: 139
Joined: Wed Feb 23, 2022 9:43 pm
Contact:

Re: Music Player UI screen help

#15 Post by lsf22 »

Question, is it a bug or is it my placement of coding renpy.restart_interaction that causes the track names not to refresh right? I'm using the latest Renpy now at version 8.2.0. When I commented out the fade out code, it works smoother and the track names refresh without error so far when using the buttons.

Code: Select all

# Step 1. Create a MusicRoom instance.
    mr = MusicRoom(channel='music')#,fadeout=1.0)

Another thing is that when a song ends, it does not refresh the track name. Is my order of code to blame?

Code: Select all

init python:
    def get_playing(music_room):
        return renpy.music.get_playing(music_room.channel) or ""

define audios = {
    "audio/bgm_1.mp3": "Test 1 Track", "audio/bgm_2.mp3": "Test 2 Track", "audio/bgm_3.mp3": "Test 3 Track",
    "audio/bgm_4.mp3": "Test 4 Track", "audio/bgm_5.mp3": "Test 5 Track", "audio/bgm_6.mp3": "Test 6 Track",
    "audio/bgm_7.mp3": "Test 7 Track"
}

# images defined for imagemap
image imagemap_hover_img = "/images/ui_icons/Image_map_icon_music_1_hover.png"
image imagemap_idle_img = "/images/ui_icons/Image_map_icon_music_1_idle.png"

init python:

    # Step 1. Create a MusicRoom instance.
    mr = MusicRoom(channel='music') #, fadeout=1.0)

    # Step 2. Add music files.
    mr.add("audio/bgm_1.mp3", always_unlocked=True)
    mr.add("audio/bgm_2.mp3", always_unlocked=True)
    mr.add("audio/bgm_3.mp3", always_unlocked=True)
    mr.add("audio/bgm_4.mp3", always_unlocked=True)
    mr.add("audio/bgm_5.mp3", always_unlocked=True)
    mr.add("audio/bgm_6.mp3", always_unlocked=True)
    mr.add("audio/bgm_7.mp3", always_unlocked=True)

screen UI_Icon_MusicPlayer():
    $ track_name = audios.get(get_playing(mr), "")

    imagemap:
        xalign 0.01
        yalign 0.01
        ground "/images/ui_icons/Image_map_icon_music_1_idle.png"
        idle "imagemap_idle_img"
        hover "imagemap_hover_img"
        hotspot (297, 322, 91, 102) action mr.Play(), renpy.restart_interaction
        hotspot (183, 324, 102, 99) action mr.RandomPlay(), renpy.restart_interaction
        hotspot (405, 328, 89, 97) action mr.Stop(), renpy.restart_interaction
        hotspot (528, 322, 131, 106) action mr.Next(), renpy.restart_interaction
        hotspot (38, 320, 135, 110) action mr.Previous(), renpy.restart_interaction

    hbox:
        xpos 0.05
        ypos 0.09
        text "[track_name]" color 'f0f4ff'
Attachments
music not refreshing upon next track.mkv.zip
MKV format clip of music not refreshing after playing next track
(497.84 KiB) Downloaded 11 times
Test Music Player UI 3.zip
Project folder
(2.83 MiB) Downloaded 10 times

Post Reply

Who is online

Users browsing this forum: piinkpuddiin