Giuppox
Giuppox

Reputation: 1621

pygame.mixer.Sound().play() gives no error but actually does not play

I am trying to play a .wav file with pygame

import pygame
pygame.mixer.init()
s = pygame.mixer.Sound('absolute path to file')
s.play()

it gives no error, but it seems like it doesn't play anything.
This is the complete output

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html

installed pygame via pip in a conda python 3.6 enviroment. mac book pro 2019 with macOS Mojave

Upvotes: 1

Views: 351

Answers (2)

Giuppox
Giuppox

Reputation: 1621

As bashBediam said the problem was that the code was ending not giving time to the audio to play. i fixed that doing like this.

import pygame
pygame.mixer.init()
s = pygame.mixer.Sound(path)
channel = s.play()
while channel.get_busy():
     pygame.time.wait(100)

Upvotes: 2

bashBedlam
bashBedlam

Reputation: 1500

Add a line at the end :

input ('Press enter to continue.')

and you will hear it. Your script is ending before it has a chance to play the sound.

Upvotes: 1

Related Questions