Thursday 10 December 2015

Pygame: playing sounds

In this post, I am going to explain how to play sounds using pygame. For this tutorial I am going to use simple OGG audio file. OGG is an open, free container format for digital multimedia.

audioFile = pygame.mixer.Sound("audio.ogg")
Above statement loads the multi media file audio.ogg and create a new Sound object from a file or buffer object.

audioFile.play()

begin sound playback
# import pygame
import pygame

# initialize game engine
pygame.init()

window_width=500
window_height=500

animation_increment=10
clock_tick_rate=20

# Open a window
size = (window_width, window_height)
screen = pygame.display.set_mode(size)

# Set title to the window
pygame.display.set_caption("Hello World")

dead=False
audioFile = pygame.mixer.Sound("/Users/harikrishna_gurram/Desktop/audio.ogg")

# Initialize values for color (RGB format)
WHITE=(255,255,255)

clock = pygame.time.Clock()

while(dead==False):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            audioFile.play()

    screen.fill(WHITE)
    pygame.display.flip()
    clock.tick(clock_tick_rate)


Above program play the audi file whenever mouse click happen on window.

References




Previous                                                 Next                                                 Home

No comments:

Post a Comment