[Solved] Music player not refreshes playing song name, plays randomly and plays last song after stop

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
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

[Solved] Music player not refreshes playing song name, plays randomly and plays last song after stop

#1 Post by kostek00 »

Code: Select all

init python:
    playerList = {
        "bgm/M01.ogg":["images/bgm/M01.png"],
        "bgm/M02.ogg":["images/bgm/M02.png"],
        "bgm/M03.ogg":["images/bgm/M03.png"],
        "bgm/M04.ogg":["images/bgm/M04.png"],
        "bgm/M05.ogg":["images/bgm/M05.png"],
        "bgm/M06.ogg":["images/bgm/M06.png"],
        "bgm/M07.ogg":["images/bgm/M07.png"],
        "bgm/M08.ogg":["images/bgm/M08.png"],
        "bgm/M09.ogg":["images/bgm/M09.png"],
        "bgm/M10.ogg":["images/bgm/M10.png"],
        "bgm/M11.ogg":["images/bgm/M11.png"],
        "bgm/M12.ogg":["images/bgm/M12.png"],
        "bgm/M13.ogg":["images/bgm/M13.png"],
        "bgm/M14.ogg":["images/bgm/M14.png"],
        "bgm/M15.ogg":["images/bgm/M15.png"],
        "bgm/M16.ogg":["images/bgm/M16.png"],
        "bgm/M17.ogg":["images/bgm/M17.png"],
        "bgm/M18.ogg":["images/bgm/M18.png"],
        "bgm/M19.ogg":["images/bgm/M19.png"],
        "bgm/M20.ogg":["images/bgm/M20.png"],
        "bgm/M21.ogg":["images/bgm/M21.png"]
    }

    mr = MusicRoom(fadeout=0.5, shuffle=False)

    for track in playerList:
        mr.add(track, always_unlocked=True)

Code: Select all

screen extra:
    tag menu
    add "images/gui/menu_ground.png"

    modal True

    default extra_gallery = "cg"
    default cg_page = 1
    default memories_page = 1

    imagemap:
        ground "images/gui/extra_ground.png"
        idle "images/gui/extra_idle.png"
        hover "images/gui/extra_hover.png"
        selected_idle "images/gui/extra_selected_idle.png"
        selected_hover "images/gui/extra_selected_hover.png"

        alpha False

        hotspot(302,53,31,27) action mr.Previous()
        hotspot(358,53,25,27) action mr.Play() selected renpy.music.is_playing()
        hotspot(406,53,27,27) action mr.Stop()
        hotspot(454,53,32,27) action mr.Next()
        hotspot(25,534,149,40) action Return()

    if renpy.music.get_playing() is not None:
        $ currentTrack = playerList[renpy.music.get_playing()]
    image (currentTrack[0]) pos(509,43)

    on "replaced" action Play("music", "bgm/M01.ogg")
This is the piece of code for music player (found it on this forum and modified to accept images). There are 3 bugs with it:
1. I'm showing song name with image. When song is changed it's name isn't until I press one of the buttons in that screen that are using "SetScreenVariable()". Refresh? (They aren't shown in code above but they are there.)
2. When I use next or previous button they pick somewhat randomish song. I even wrote "shuffle=False" but still the same. What I found is they are played in this order: "1,6,12,18,8,7,14,15,21,19,3,2,16,20,11,4,17,9,13,10,5". No idea why.
3. When I press stop button and than play last song from numbers above is played (5th in whole list).

Any ideas how any of those can be fixed? 1st and 3rd point is probably the most important for me. I don't have list of all songs in game so users won't know what order they are played.

(I don't know python.)
Last edited by kostek00 on Sun Jun 03, 2018 2:46 pm, edited 1 time in total.

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

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#2 Post by Ocelot »

1) You probably want to use action parameter of add method. It is even given example of doing something similar in documentation: "For example, These actions is used to change a screen or background, description by the playing file"
https://www.renpy.org/doc/html/rooms.html#MusicRoom.add

2) playerList is a Dictionary. Those are not ordered: keys are stored in seemingly random order. YOu can change it to list of tuples and transform it to dictionary after adding tracks in Music Room, or keep it a Dict and copy keys into list to sort before adding to Music Room.

3) Same thing. 5 is the first entry in dictionary, it seems.
< < insert Rick Cook quote here > >

User avatar
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#3 Post by kostek00 »

1. Do you know example usage for this? I don't know how to use it in my code.

2, 3. Oh dear. That's way beyond my understanding.

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

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#4 Post by Ocelot »

For 2 it is easy:

Code: Select all

init python:
    playerList = [ # Square brackets denote a list
        ("bgm/M01.ogg", "images/bgm/M01.png"), # Parentheses — tuple (immutable sequence of values)
        ("bgm/M02.ogg", "images/bgm/M02.png"), # Here you have a list of pairs basically
        # . . . Rest of entries
    ]

    mr = MusicRoom(fadeout=0.5, shuffle=False)

    for (track, image) in playerList:
        mr.add(track, always_unlocked=True)

    playerList = dict(playerList) # Transform it into dict so you can do playerList[something]
For 1 it would be something like:

Code: Select all

# In init:
mr.add(track, always_unlocked=True, action=SetVariable("my_image", image)) # Look into previous snippet to see  where image came from

default my_image = . . . # Image for no song playing

# in your screen:
add my_image
< < insert Rick Cook quote here > >

User avatar
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#5 Post by kostek00 »

For the first block I actually did something simillar but it wasn't working because I didn't changed brackets. :P

With only first code block in place where image should be I have "Image "i" not found."

By the way what should I change for "add my_image" because this part of code is for showing image from dictionary:

Code: Select all

    if renpy.music.get_playing() is not None:
        $ currentTrack = playerList[renpy.music.get_playing()]
    image (currentTrack[0]) pos(509,43) # to be more precise this one line shows them
When I also apply second part of code image for no song playing is always shown If I change it to:

Code: Select all

    image (my_image) pos(509,43)

Whole code after change in case I wrote something wrong:

Code: Select all

    playerList = [
        ("bgm/M01.ogg", "images/bgm/M01.png"),
	# rest of list
        ("bgm/M21.ogg", "images/bgm/M21.png")
    ]

    mr = MusicRoom(fadeout=0.5)

    for (track, image) in playerList:
        mr.add(track, always_unlocked=True, action=SetVariable("my_image", image))

    playerList = dict(playerList)


screen extra:
    tag menu
    add "images/gui/menu_ground.png"

    modal True

    default extra_gallery = "cg"
    default cg_page = 1
    default memories_page = 1
    default my_image = "images/bgm/M15.png" # took some random image just to test

    if renpy.music.get_playing() is not None:
        $ currentTrack = playerList[renpy.music.get_playing()]
    image (currentTrack[0]) pos(509,43)

    on "replaced" action Play("music", "bgm/M01.ogg")
Sorry for bothering again.

User avatar
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#6 Post by kostek00 »

I tried to change code a little bit to see if it change something and come with this but it's the same. It always uses default image.

Code: Select all

    mr = MusicRoom(fadeout=0.5)
    # Music player
    mr.add("bgm/M01.ogg", always_unlocked=True, action=SetVariable("my_image", "images/bgm/M01.png"))
    # other songs
    mr.add("bgm/M21.ogg", always_unlocked=True, action=SetVariable("my_image", "images/bgm/M21.png"))


screen extra:
    tag menu
    add "images/gui/menu_ground.png"

    modal True

    default extra_gallery = "cg"
    default cg_page = 1
    default memories_page = 1
    default my_image = "images/bgm/M18.png" # example to see if it's working

    on "replaced" action Play("music", "bgm/M01.ogg")

    imagemap:
        ground "images/gui/extra_ground.png"
        idle "images/gui/extra_idle.png"
        hover "images/gui/extra_hover.png"
        selected_idle "images/gui/extra_selected_idle.png"
        selected_hover "images/gui/extra_selected_hover.png"

        alpha False

        hotspot(302,53,31,27) action mr.Previous()
        hotspot(358,53,25,27) action mr.Play() selected renpy.music.is_playing()
        hotspot(406,53,27,27) action mr.Stop()
        hotspot(454,53,32,27) action mr.Next()
        hotspot(25,534,149,40) action Return()

    add my_image pos(509,43)
Just out of curiosity I tried something like this:

Code: Select all

mr.add("bgm/M01.ogg", always_unlocked=True, action="images/bgm/M01.png")
or this
mr.add("bgm/M01.ogg", always_unlocked=True, action=("images/bgm/M01.png"))
But it always throws error that "'unicode' object is not callable. It's clear that i'm not programist and all I can do is try and see if magic happens. :lol:

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

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#7 Post by Ocelot »

my_image should be global image, not screen one. Altirnatively, you can try to replace SetVariable with SetScreenVariable
< < insert Rick Cook quote here > >

User avatar
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#8 Post by kostek00 »

What do you mean by global image?

Edit: *facepalm*

I just realized my stupidity. It was supposed to be outside "screen extra:" block. Ech...

Unfortunately I'm getting this error:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/screens.rpy", line 244: invalid syntax
    default my_image = "images/bgm/M21.png"
                    ^
    

Ren'Py Version: Ren'Py 6.99.14.3.3347
Wed May 16 21:05:46 2018

User avatar
kostek00
Regular
Posts: 131
Joined: Wed Mar 28, 2018 5:54 pm
Contact:

Re: Music player not refreshes playing song name, plays randomly and plays last song after stop

#9 Post by kostek00 »

As I was making "default" values for my options I realized what I was doing wrong and where "default my_image = "images/bgm/M21.png" should be placed. It works perfectly.

Thank you for your help.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Ocelot