[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Open Sounds From URL



You don't need StringIO there. It is just converting a string into a file object. urllib2.urlopen() returns a file object.

So this will work also:

#! /usr/bin/env python

import urllib2, time
from pygame import mixer
from cStringIO import StringIO

#Read the sound from the url
snd = urllib2.urlopen("http://pymike93.googlepages.com/shoot.wav";)

#Convert to a sound!
mixer.init()
sound = mixer.Sound(snd)
sound.play()

#Pause while you listen to a sound loaded from an online database!
time.sleep(1)


PyMike wrote:
Hey guys! I figured out how to open sounds from urls! Enjoy!

#! /usr/bin/env python

import urllib2, time
from pygame import mixer
from cStringIO import StringIO

#Read the sound from the url
f = urllib2.urlopen("http://pymike93.googlepages.com/shoot.wav";).read()

#Convert to a file
snd = StringIO(f)

#Convert to a sound!
mixer.init()
sound = mixer.Sound(snd)
sound.play()

#Pause while you listen to a sound loaded from an online database!
time.sleep(1)