Pygame - Playing Music


The mixer also has a special streaming channel for music playback and is accessed through the pygame.mixer.musicpygame module for controlling streamed audio module. The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.

First of all, we need to load the music from a music file. Pygame can load WAV, MP3, or OGG files.

pygame.mixer.music.load(filename or object)

This will load a music filename/file object and prepare it for playback. If a music stream is already playing it will be stopped. This does not start the music playing. The playback is controlled by following functions −

play(loops=0, start=0.0, fade_ms = 0)

This will play the loaded music stream. If the music is already playing it will be restarted. loops argument tells how many times to repeat the music. The music repeats indefinitely if this argument is set to -1. start denotes the music starts playing from. The position as time in seconds. fade_ms argument makes the music start playing at 0 volume and fade up to full volume over the given time.

Other useful functions are given below −

rewind() Resets playback of the current music to the beginning.
stop() Stops the music playback if it is currently playing. It Won't Unload the music.
pause() Temporarily stop playback of the music stream.
unpause() This will resume the playback of a music stream after it has been paused.
fadeout(time) Fade out and stop the currently playing music.
set_volume(volume) Set the volume of the music playback.
set_pos(pos) This sets the position in the music file where playback will start.

In the following program, a music file starts playing on clicking PLAY button. The PAUSE button acts as a toggle to pause/unpause play. Click on STOP stops the playback.

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300)) 20. Pygame — Playing music
done = False
white = (255,255,255)
pygame.mixer.music.load("mario_theme.wav")
font = pygame.font.SysFont("Arial", 14)
text1=font.render(" PLAY ", True, white)
text2=font.render(" PAUSE ", True, white)
text3=font.render(" STOP ", True, white)
rect1 = text1.get_rect(topleft=(10,10))
rect2 = text2.get_rect(topleft= (100,10))
rect3 = text3.get_rect(topleft= (200,10))
bg = (127,127,127)
psmode=True
screen = pygame.display.set_mode((400,300))
screen.fill(bg)
while not done:
   for event in pygame.event.get():
      screen.blit(text1, rect1)
      pygame.draw.rect(screen, (255,0,0),rect1,2)
      screen.blit(text2, rect2)
      pygame.draw.rect(screen, (255,0,0),rect2,2)
      pygame.draw.rect(screen, (255,0,0),rect3,2)
      screen.blit(text3, rect3)

      if event.type == pygame.QUIT:
         done = True
      if event.type == pygame.MOUSEBUTTONDOWN:
         if rect1.collidepoint(event.pos):
            pygame.mixer.music.play()
         if rect2.collidepoint(event.pos):
            if psmode==True:
               pygame.mixer.music.pause()
               psmode=False
            else:
               if psmode==False:
                  pygame.mixer.music.unpause()
                  psmode=True
            if rect3.collidepoint(event.pos):
               pygame.mixer.music.stop()
   pygame.display.update()
Advertisements