[Tutor] sound implementation problems

Jim Mooney cybervigilante at gmail.com
Thu Jun 13 19:56:06 CEST 2013


I'll assume you're using Windows. If not, ignore this ;')

winsound on windows is Python native, much simpler, and always works. After
importing it you can type help(winsound) to see its controls. But here is
the usage for a wav file (it doesn't play mp3s)  There is no rule you have
to use the pygame functionality, which is more complex, to get a sound, if
that's all you want.

import winsound
winsound.PlaySound('c:/python33/media/wtf.wav', 1)

Make sure you end with the "1". The helpfile doesn't mention what to use as
the second parameter, but 1 works fine. And one other thing that tripped me
up. If you're using an IDE or editor, mine has the unfortunate habit of
loading in its own directory, and having no option to automatically access
files from my  program directory. It saves a program into the last used
directory, but looks for sounds in its own directory. Ugh. When I thought
my program was accessing a wav from my standard program directory, it was
really trying to find it in the PyScripter directory ;')

So use the Full Path to your sound file, and avoid that possible problem. I
have a startup script that now stays in my program directory, though. Of
course, if you have an IDE or editor that lets you set the default
directory that's no problem.

If your editor doesn't do default directories but has startup scripts this
will work (changing the directoy in chdir to your system, of course)

import os
os.chdir('c:/python33/jimprogs')
del(os)


Jim

On 13 June 2013 10:21, jessica peters <howewriter2000 at yahoo.com> wrote:

> Hi
>
> I'm about 2 yrs into studying Python - started with "Hello World", and I'm
> working with v 2.5.1 right now.  The past year I've begun trying to write
> my own interactive fiction.  That works pretty well, but now I'm attempting
> to put some music into programs (I thought background music would be good),
> and I'm running into roadblocks.
>
> I've tried several different things for this, and come up with either my
> text that comes to a halt eventually at an error message (can't read from
> the files or mixer isn't initialized are the most common ones), or a
> completely blank screen with no sound.  I've tried both .mp3 files and .wav
> ones, neither works for this.
>
> Here's the most recent code I've attempted:
>
> import pygame , sys
> import random
> size=[500,500]
> def run(self):
>     import pygame.mixer
>     pygame.mixer.init(22050, -16, 2, 4096)
>     self.sound.seek(0)
>     snd = pygame.mixer.Sound(self.sound)
>     pygame.mixer.Sound.play("bach-cello-suite-1.wav")
>     musicPlaying = True
>
> Any ideas would  be appreciated.  Thanks.
>
> my website: http://jahowe.com
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Jim
A noun is just a verb with the hiccups
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130613/df20d7c5/attachment.html>


More information about the Tutor mailing list