473,392 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,392 software developers and data experts.

Synchronous/Asynchrnous Audio play with pymedia

Hello,

I'm developing a piece of software to assist illiteraate adults to learn to
read. I'm trying to figure out how, if possible, to make audio playback
asynchrnous but still controllable. I'm using python 2.4 with pymedia on
XP. I started out with the example in the tutorials section of the pymedia
website. The pymedia docs imply to me that playback using Output's play()
method should already be asynchronous and controllable. I judge this
because of the presence of the pause() and unpause() methods. However, when
I run the example I find that play() in fact is not returning until an
entire frame has been played through the sound card. This isn't entirely a
problem since the frames are small, but I would prefer to give the entire
sound file to the class and let it go on playing while I continue with other
processing. If, at some point during playback, I wish to pause, unpause or
completely stop playback, I would like, again, to be able to make
asynchronous calls to do so. Doe anybody know if this is possible? Am I
using pymedia incorrectly?

I've attempted to construct a threaded class to handle my problem, but I'm a
novice with threads and It's not working properly. To use this class, first
make an instance. To assign an mp3 file for playback use myInst.select(
myFileObj ). To begin playback or to unpause use: myInst.play( ). to
pause use: myInst.pause( ). to stop use: myInst.stop( ). Once stopped,
myInst.play( ) should restart playback from the beginning. The problem is
that when I stop(), restarting with play() doesn't work. Instead, the
thread silently terminates.

Thanks for any help you can offer. I've been trying to solve this on my own
for days now and just know it shouldn't be this hard.


import time
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec
import threading, Queue

class AudioPlayer( threading.Thread ):
# States
PLAY = 1
PAUSE = 2
STOP = 3

def __init__( self ):
threading.Thread.__init__( self )

self.setDaemon( 1 )
self.requestQueue = Queue.Queue( )
self.start( )

def select( self, filelikeObj ):
self.requestQueue.put( filelikeObj )

def play( self ):
self.requestQueue.put( AudioPlayer.PLAY )

def pause( self ):
self.requestQueue.put( AudioPlayer.PAUSE )

def stop( self ):
self.requestQueue.put( AudioPlayer.STOP )

def run( self ):
state = AudioPlayer.STOP
file = None
snd = None

while True:
if (state == AudioPlayer.PLAY) and self.requestQueue.empty( ):
if not snd:
cparams= { 'id': acodec.getCodecID( 'mp3' ) }
codec = acodec.Decoder( cparams )
file.seek( 0 )
bytes = file.read( 8192 )
frame = codec.decode( bytes )
snd = sound.Output( frame.sample_rate, frame.channels,
sound.AFMT_S16_LE )
else:
bytes = file.read( 512 )
if len(bytes) > 0:
frame = codec.decode( bytes )

if frame:
snd.play( frame.data )
else:
if snd:
snd.stop( )
snd = None
state = AudioPlayer.STOP
else:
msg = self.requestQueue.get( )

if msg in ( AudioPlayer.PAUSE, AudioPlayer.PLAY ):
state = msg
else:
if snd:
snd.stop( )
snd = None
state = AudioPlayer.STOP
if msg != AudioPlayer.STOP:
file = msg

# Test application
if __name__ == '__main__':
import time
player = AudioPlayer( )
snd = open( 'music.mp3', 'rb' )
player.select( snd )
player.play( )
time.sleep( 3 )

player.pause( )
time.sleep( 3 )
player.play( )
time.sleep( 10 )
player.stop( )
time.sleep( 3 )
player.play( )

Sep 18 '05 #1
1 4267
At 01:22 19.09.2005, Ron Provost wrote:
Hello,

I'm developing a piece of software to assist illiteraate adults to learn to
read. I'm trying to figure out how, if possible, to make audio playback
asynchrnous but still controllable. I'm using python 2.4 with pymedia on
XP.


there's a pymedia mailing list at:
https://lists.sourceforge.net/lists/.../pymedia-users
better ask there, the author of pymedia is generally very helpful in
answering question.

sven.
Sep 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bruce Bon | last post by:
The class below is intended to play a Sun audio file (.au) in the background while the main thread, which is servicing a GUI, continues without impact. It doesn't work. For a sound file that...
0
by: laredotornado | last post by:
Hello, I want to play an audio file by clicking on an audio icon and not having the page switch out underneath. Right now the code I have is ... <html> <head> <title>Dictionary:...
0
by: Nick Parker | last post by:
I'm attempting to play an mp3 file on OSX, but am running into some difficulty. When using py-mad and py-ao, I only get static with the following code (which is derived off another mailing that I...
0
by: apa7hy.spam | last post by:
this message was originally posted by someone else and closed without a proper answer. i'm reposting it in hopes that someone will provide a solution. Begin Quote: "I'm attempting to play...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
0
by: Carlos Leite | last post by:
Is there any other option to analyze audio frequencies? Or Any here try to use pymedia to a simple audio FingerPrint. A cant understand what the information the asBands() really giveme....
1
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I am using Windows Media Player to play my half second audio files one after the other using the code below... My problem comes when I play a second audio file immediately after the first one...
3
by: AWW | last post by:
Using XP and VB 2005, it seemed like a good idea to save audio in the clipboard and then play it with backgroundworker. Cannot find a good Clipboard audio example anywhere - just SetAudio and...
1
by: patelss23 | last post by:
Hello All Experts, I am quite new to Ctypes. I am using one c library and writing python bindings for it. I need to pass a character pointer to one function. I am reading one mp3 file and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.