have a game with one main screen and one pause screen. I want there to be a different music to played for each screen from where they left off previously. The following is the relevant section of my code:
while running:
if Options.want_pause:
if load_count == 0 and Options.want_Music: # clicking the button to swap screens resets load_count to 0
mpos = pygame.mixer.music.get_pos() # gets pos of music from main mainwindow
music = pygame.mixer.music.load('Pause music.mp3')
pygame.mixer.music.set_pos(ppos/1000) # plays from previous music position
pygame.mixer.music.play(-1)
load_count = 1
pausewindow(win_width, win_height, width, height)
else:
if load_count == 0 and Options.want_Music:
ppos = pygame.mixer.music.get_pos()
music = pygame.mixer.music.load('Main music.mp3')
try: #starts from beginning the first time, after which mpos will be definied
pygame.mixer.music.set_pos(mpos/1000)
except NameError:
pygame.mixer.music.play(-1)
pygame.mixer.music.play(-1)
load_count = 1
set_dirs(cur_block, width, height)
gamewindow(win_width, win_height, width, height, no)
#etc
it works fine if I don't want to resume the music and just start all over from the beginning, (i.e. if I get rid of the get_pos and set_pos lines), but the above gives me the following error:
Traceback (most recent call last): File "C:/Vijay Stuff/Coding stuff/pycharm projects/Summer projects 2020/Tetris/Tetris 1.5.2.py", line 462, in pygame.mixer.music.set_pos(mpos/1000) pygame.error: set_pos unsupported for this codec
I also tried using pygame.mixer.music.play(-1, mpos/1000) (and same for the other one) and although this doesn't give an error, it doesn't work properly. Basically it only works if I enter the exit the other screen very quickly, which is of course pointless. Is there a solution to this?
Thanks for any help :)
Edit: I found out the answer. You can create 2 separate channels:
main_channel = pygame.mixer.Channel(0)
pause_channel = pygame.mixer.Channel(1)
load a different music onto each one
main_channel.play(pygame.mixer.Sound(mymusic1), loops=-1, fade_ms=5000)
pause_channel.play(pygame.mixer.Sound(mymusic2), loops=-1, fade_ms=5000)
pause the unwanted one
pause_channel.pause()
and call unpause()/ pause() whenever necessary