×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Callback after audio file stopped playing

    Hi,

    I've looked for a way to get a notification when the file stopped playing with the new audio module in python 1.2 for S60. Does anyone know how to do that without hacking the code?

    Btw, here is a hack that does the trick: audio.Sound object has an internal object _player (see the source code of audio.py). Module uses the _player object with a callback, with 3 parameters: previous, current and error. The way that the callback is used, I can only assum that previous and current are old and new state of the player. Using the _player object directly, you can install your own callback notification:

    Code:
    #####
    def playerCallBack(previous,current,error):
        if (error!=0):
            print "Error"   
        if (previous==audio.EOpen and current==audio.EPlaying):
            print "Playing"
        elif (previous==audio.EPlaying and current==audio.EOpen):
            print "Stopped"
    
    #
    s=audio.Sound.open("BSA2.amr")
    s._player.bind(playerCallBack)
    s._player.play(0,0)
    
    #######
    this prints out
    Playing
    Stopped

    note that the first argument of s._player.play is 0 for 1 playing (if you pass 1, the file will be played twice).
    Last edited by gojkoa; 2005-10-30 at 17:17. Reason: indentation

  2. #2
    Regular Contributor
    Join Date
    Sep 2003
    Location
    Finland
    Posts
    209

    Re: Callback after audio file stopped playing

    Quote Originally Posted by gojkoa
    I've looked for a way to get a notification when the file stopped playing with the new audio module in python 1.2 for S60. Does anyone know how to do that without hacking the code?
    Currently this is not supported but hopefully we can add this for the upcoming release. I'd say this will be an optional keyword argument "call_back" for "play()".

    If you have more wishes regarding this module, please post these to this board. All feature requests are very welcomed. Currently at least these are requested:

    - volume setting;
    - playhead setting (from where does the playing start);
    - notification of end of playing (your request).

  3. #3
    Regular Contributor
    Join Date
    Jan 2005
    Location
    Bangkok, Thailand
    Posts
    148

    Re: Callback after audio file stopped playing

    - access to the waveform data (to do volume and pitch detection).
    - periodical callback on recording (so can do pitch detection and
    spectrogram, above, in realtime)

    I am thinking of a whistle detection program that repeat my whistle in midi.

  4. #4
    Regular Contributor
    Join Date
    Sep 2003
    Location
    Finland
    Posts
    209

    Re: Callback after audio file stopped playing

    Quote Originally Posted by gojkoa
    I've looked for a way to get a notification when the file stopped playing with the new audio module in python 1.2 for S60. Does anyone know how to do that without hacking the code?
    Here is a modified "audio.py" wrapper you can use to get callbacks (note: modifying directly something in a module which starts with '_' might cause tedious problems):

    Code:
    #
    # audio.py
    #
    # Copyright (c) 2005 Nokia. All rights reserved.
    #
    
    import e32
    import _recorder
    
    ENotReady = _recorder.ENotReady
    EOpen = _recorder.EOpen
    EPlaying = _recorder.EPlaying
    ERecording = _recorder.ERecording
    KMdaRepeatForever = _recorder.KMdaRepeatForever
    
    class Sound(object):
        def __init__(self):
            self._player=_recorder.Player()
        def open(filename):
            def open_cb(previous, current, err):
                callback_error[0]=err
                lock.signal()
            player=Sound()
            lock=e32.Ao_lock()
            callback_error=[0]
            player._player.bind(open_cb)
            player._player.open_file(unicode(filename))
            lock.wait()
            if callback_error[0]:
                raise SymbianError,(callback_error[0],
                                    "Error opening file: "+e32.strerror(callback_error[0]))
            return player
        open=staticmethod(open)
        def play(self, times=1, interval=0, callback=None):
            def play_cb(previous, current, err):
                #This is called first with EPlaying meaning that the playing started
                #and with EOpen meaning that the playing stopped.
                callback_error[0]=err
                if callback!=None:
                    callback(previous, current, err)
                    if current==EOpen:
                        lock.signal()
                elif (current==EPlaying or current==EOpen) and callback==None:
                    lock.signal()
            if self.state()!=EOpen:
                raise RuntimeError,("Sound not in correct state, state: %d" % (self.state()))
            lock=e32.Ao_lock()
            callback_error=[0]
            self._player.bind(play_cb)
            if not times==KMdaRepeatForever:
                times=times-1
            self._player.play(times, interval)
            lock.wait()
            if callback_error[0]:
                raise SymbianError,(callback_error[0],
                                    "Error playing file: "+e32.strerror(callback_error[0]))
        def record(self):
            def rec_cb(previous, current, err):
                callback_error[0]=err
                lock.signal()
            if self.state()!=EOpen:
                raise RuntimeError,("Sound not in correct state, state: %d" % (self.state()))
            lock=e32.Ao_lock()
            callback_error=[0]
            self._player.bind(rec_cb)
            self._player.record()
            lock.wait()
            if callback_error[0]:
                raise SymbianError,(callback_error[0],
                                    "Error while recording: "+e32.strerror(callback_error[0]))
        def stop(self):
            self._player.stop()
        def close(self):
            self._player.close_file()
        def state(self):
            return self._player.state()
    Example usage in my 6600:

    Code:
    Python 2.2.2 (#0, Oct 19 2005, 10:52:24)
    [GCC 2.9-psion-98r2 (Symbian build 546)] on symbian_s60
    Type "copyright", "credits" or "license" for more information.
    Type "commands" to see the commands available in this simple line editor.
    >>> import audio
    >>> def cb(prev, cur, err):
    ...     print ("previous: %d current: %d error: %d" % (prev, cur, err))
    ...
    >>> p=audio.Sound.open("c:\\nam.amr")
    >>> p.play(callback=cb)
    previous: 1 current: 2 error: 0
    previous: 2 current: 1 error: 0
    >>>

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Re: Callback after audio file stopped playing

    Quote Originally Posted by otsov
    Currently this is not supported but hopefully we can add this for the upcoming release. I'd say this will be an optional keyword argument "call_back" for "play()".

    If you have more wishes regarding this module, please post these to this board. All feature requests are very welcomed. Currently at least these are requested:

    - volume setting;
    - playhead setting (from where does the playing start);
    - notification of end of playing (your request).
    I've found another thing on the same subject - callback does not get called when you call stop(). So, making a blocking wait until the callback is called does not guarantee that the method will ever finish. I guess that calling the bound callback from stop() is logical - it is a state change.

    gojko

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    Re: Callback after audio file stopped playing

    Feature request:
    Access to the duration of the current clip.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Re: Callback after audio file stopped playing

    Feature Request:
    (though not necessarily pertinent to Callback features)
    1- I'd like to be able to either pass a file object to _player.open_file (instead of a filename) or even my own buffer. I'm trying to stream encoded audio but will ultimately fail if required to have a complete closed file ready to pass on to the audio subsystem.

    2- access to the source for all system pyd?

Similar Threads

  1. Sample code for playing AMR file.
    By khurshed79 in forum Symbian Media (Closed)
    Replies: 0
    Last Post: 2005-07-27, 12:36
  2. Replies: 1
    Last Post: 2005-05-26, 14:22
  3. Problems playing some wav audio formats using MMAPI (WTK22)
    By sandromancuso in forum Mobile Java General
    Replies: 0
    Last Post: 2005-01-18, 17:03
  4. Audio Playing on phone : Query
    By sshuhangi in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2003-07-04, 06:30
  5. Mixing and Matching of existing audio file?
    By hiteshw in forum Symbian
    Replies: 0
    Last Post: 2002-02-20, 02:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×