1

I want to play sound with python pygame

I tried:

import pygame

pygame.init()

pygame.mixer.music.load("bellhigh.wav")

pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
      continue

This give me an error saying:

error: Unkown WAVE data format

import pygame

pygame.mixer.init()
s = pygame.mixer.Sound("bellhigh.wav")
s.play()

This code run without any error but doesn't play any sound at all.

I followed this post: Play a Sound with Python

In a comment by Domster: From the manual: "The mixer module must be initialized like other pygame modules, but it has some extra conditions. The pygame.mixer.init - initialize the mixer module function takes several optional arguments to control the playback rate and sample size. Pygame will default to reasonable values, but pygame cannot perform Sound resampling, so the mixer should be initialized to match the values of your audio resources." - that might be why your resources load incorrectly

Any help would be much appreciated thanks.

Community
  • 1
  • 1
RamHS
  • 750
  • 6
  • 24

1 Answers1

0

Update: Ah i see you are on raspbian... so am i.. and below worked for me..

==

I copied chord.wav from my windows PC in "C:\Windows\Media\chord.wav" and used it on similar to code to your above one..

OR try to use the command shell process..

aplay is a sound player... choose any other pick which is available for your machine to play sound.

from subprocess import call
call(["aplay", "-q", "chord.wav"])

This will at least help you to diagnose the problem..

--

To set volume in raspberry pi, use this:

amixer set PCM -- 400 (use 400 for max volume) negative numbers are less volume e.g 200, 100, -100, -1000, -4000

source: http://blog.paddlefish.net/?p=895

ihightower
  • 3,093
  • 6
  • 34
  • 49