1

So, first of all, my overall goal is to play a different background music depending on where the player is on the map. I want one track to fade out and the other to fade in when the player reaches a certain point.

Here are my problems:

  • I've created a "Fader" class, with the help of Fade Between Two Music Tracks in-progress in Pygame. This allows me to fade tracks in and out, but it doesn't specify how to adapt the code to use pygame.mixer.music instead of pygame.mixer.Sound.

  • If I play both tracks as a Sound, on different Channels, the program slows wayyyyyy down - rendering it unusable. Research has shown that this is because music is played one bit at a time, while sounds are loaded in all at once and then played - which obviously takes quite a bit of processing power.

  • However, I can't seem to play them as music - and here we come to the heart of my problem. This is my Fader class written to play the music as a Sound:

    class Fader(object):
        def __init__(self, filename):
            self.sound = pygame.mixer.Sound(filename)
            # the speed at which it will fade
            self.increment = 0.5
            # the volume to which it will fade
            self.next_vol = 1
    
        def fade_to(self, new_vol):
            self.next_vol = new_vol
            curr_volume = self.get_volume()
            if self.next_vol > curr_volume:
                 self.set_volume(curr_volume + self.increment)
            elif self.next_vol < curr_volume:
                 self.sound.set_volume(curr_volume - self.increment)
    
  • This doesn't work, for reasons stated above. However, I cannot for the life of me achieve the same effect with music. The code written to play music as Music is the same except for line 3, which is now:

    self.sound = pygame.mixer.music(filename)
    
  • I then get an error message saying "pygame.mixer.music is not callable." I understand that to mean that I'm not creating an object with that line, so I tried:

    self.sound = pygame.mixer.music.load(filename)
    
  • ...as I understood that to be the line that creates the Music object. However, here I get "NoneType object has no attribute 'play'" when I try to play the track.

I can't seem to figure this out, no one else seems to have this problem, and - go figure - the pygame website is down, so I can't look at the docs.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

according to the documentation on https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load

The pygame.mixer.music.load(filename) function loads the music stream into the mixer , and returns None

so to play the sound, replace

self.sound = pygame.mixer.music.load(filename)

with pygame.mixer.music.load(filename)

followed by pygame.mixer.music.play(filename)

Meringoo
  • 76
  • 4