pygame-ce
pygame-ce copied to clipboard
``pygame.Music``
trafficstars
Hello everyone,
Proud to propose you a first version of the pygame.Music object after days of work.
Tasks not finished yet :
- [ ] Reduce the need for SDL_Mixer +2.6.0 functions
- [x] Add macros so github tests can breath
- [ ] Return -1.0 for unsupported formats (wav, midi ...) when getting the duration and other things
- [ ] Add doc
- [ ] Add tests
- [ ]
fadeinandfadeoutproperties - [ ]
playingproperty - [ ]
MUSICENDEDevent when the music is stopped / ends. ... and many bugs hidden under the carpet to fix ...
Current test code (Use your own musics, who knows, there might be some issues):
import pygame
def main():
pygame.init()
music = pygame.Music("music1.mp3")
music2 = pygame.Music("music2.mp3")
musics = [music, music2]
index = 0
screen = pygame.display.set_mode([800, 800])
font = pygame.Font(None)
musics[index].play()
clock = pygame.Clock()
running = True
while running:
dt = clock.tick() / 1000
screen.fill("white")
text = f"""
fps = {round(clock.get_fps(), 2)}fps
Title : {musics[index].title}
Artist : {musics[index].artist}
Album : {musics[index].album}
Copyright : {musics[index].copyright}
music position = {round(musics[index].position, 2)}s
music duration = {round(musics[index].duration, 2)}s
music volume = {round(musics[index].volume * 100)}%
music paused = {musics[index].paused}
music ended = {musics[index].ended}
"""
screen.blit(font.render(text, True, "black"))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
musics[index].stop()
elif event.key == pygame.K_r:
musics[index].rewind()
elif event.key == pygame.K_p:
musics[index].play(fade_in=3)
elif event.key == pygame.K_a:
musics[index].position -= 5
elif event.key == pygame.K_z:
musics[index].position += 5
elif event.key == pygame.K_o:
musics[index].paused = not musics[index].paused
if event.key == pygame.K_UP:
musics[index].volume += 0.05
elif event.key == pygame.K_DOWN:
musics[index].volume -= 0.05
elif event.key == pygame.K_LEFT:
index = (index - 1) % len(musics)
elif event.key == pygame.K_RIGHT:
index = (index + 1) % len(musics)
elif event.type == pygame.TEXTINPUT:
try:
music_index = int(event.text)
index = pygame.math.clamp(music_index - 1, 0, 1)
except:
continue
pygame.quit()
if __name__ == "__main__":
main()