rTv743Mq
Posts: 20
Joined: Sat Aug 17, 2013 11:41 pm

Audio problem

Sat Mar 22, 2014 12:17 am

I wrote a short program in IDLE and it works fine when I run it in IDLE. But when I try the same program from the terminal command prompt it doesn't work, sudo python audiotest.py, after I press enter it goes back to the command prompt. No error message. Here's the program named audiotest.py
import pygame
pygame.init()
asound = pygame.mixer.Sound("police_s.wav")
achannel = pygame.mixer.Channel(2)
achannel.stop()
achannel.play(asound)

I just want to add some audio to my other programs, thank you.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Audio problem

Sat Mar 22, 2014 4:05 am

Your program quits right after it has started to play the sound. Pygame won't keep the program alive just because a sound is still playing. I'm not sure why it does that when started from Idle. (I don't use Idle.) To hear the sound, add a time.sleep() command at the end of the of the program:

Code: Select all

import pygame, time
pygame.init()
asound = pygame.mixer.Sound("police_s.wav")
achannel = pygame.mixer.Channel(2)
achannel.stop()
achannel.play(asound)
time.sleep(10)

rTv743Mq
Posts: 20
Joined: Sat Aug 17, 2013 11:41 pm

Re: Audio problem

Sat Mar 22, 2014 8:50 pm

Thank you very much. Adding a sleep line was all it needed!

Return to “Beginners”