home  |  docs  |  features  |  forum  |  contact  search  |  site map 
  PyMedia
Features
News
  PyMedia
PyCar
  Documentation
Mailing List
FAQ

Forum
Links

  Tutorial chapter
 

From simple to complex you ask ? Not at all. We just combine everything we had in the previous 2 examples together to have something usefull. The simple audio player.

 
 
You have music files in your collection: mp3, ogg, wma, aac, ac3 and you want to play them your way. Well, it is all simple. All you need is:
  • Open demuxer
  • Open audio decoder
  • Open sound output
  • Demultiplex data
  • Decompress data
  • Play uncompressed data through sound object
As you can see all the above was already discussed before in the previous sections.

 
 
All you have to know is the file name. Then you can extract it's extension and open the decoder:
import pymedia.audio.acodec as acodec
sName='YOUR FILENAME'
dec= acodec.Decoder( str.split( sName, '.' )[ -1 ].lower() )

Now you need to know which parameters to pass to the sound object in order to open it. In most cases you'll need to parse first 8Kb( for WMA it could be up to 30Kb ) of compressed data by decoder so it returns at least 1 uncompressed frame:
f= open( sName, 'rb' )
s= f.read( 8192 )
r= dec.decode( s )

In the case when the decoder has enough data to decompress, it'll return Frame with all essential parameters set. If it returns None, you need to pass more data to it by reading the file further and passing it to the decoder. Once you've got the first decompressed frame:
import pymedia.audio.sound as sound
snd= sound.Output( r.sample_rate, r.channels, sound.AFMT_S16_LE )

Now get ready to hear some noise.
 
 
Once everything been set up for playing, you need a simple loop to read and play the whole file.
while len( s )>0:
if r: snd.play( r.data )
s= f.read( 512 )
r= dec.decode( s )

import time
while snd.isPlaying(): time.sleep( .05 )

Hear that sound ? How's that work for you ?
Of course there are million of ways to write that loop in Python, just use the one you like and have it the way you want !
 
 
Click here to get the fully functional example.
 
SourceForge.net Logo home | docs | features
contact | News | Mailing List
Forum
 
 
 

2004 PyMedia© - opens up multimedia for everyone

Webmaster