Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/audio.py
blob: 09522bb57f684d4c93be08b901dd92a58ded2850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#    Copyright (C) 2006, 2007, 2008 One Laptop Per Child
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gst
import logging

_logger = logging.getLogger('memorize-activity')

class Audio(object):
    def __init__(self):
        self._player = gst.element_factory_make('playbin', 'player')
        fakesink = gst.element_factory_make('fakesink', 'my-fakesink')
        self._player.set_property('video-sink', fakesink)
        self._playing = None

        bus = self._player.get_bus()
        bus.add_signal_watch()
        bus.connect('message', self._gstmessage_cb)

    def play(self, filename=None):
        if filename:
            _logger.debug('play audio %s' % filename)
            self._player.set_property('uri', 'file://' + filename)
            self._player.set_state(gst.STATE_NULL)
        elif self._playing == None:
            return
        else:
            _logger.debug('continue audio')

        self._player.set_state(gst.STATE_PLAYING)
        self._playing = True

    def pause(self):
        if self._playing != None:
            _logger.debug('pause audio')
            self._player.set_state(gst.STATE_PAUSED)
            self._playing = False

    def stop(self):
        self._player.set_state(gst.STATE_NULL)

    def _gstmessage_cb(self, bus, message):
        message_type = message.type

        if message_type in (gst.MESSAGE_EOS, gst.MESSAGE_ERROR):
            self._player.set_state(gst.STATE_NULL)
            self._playing = None
            _logger.debug('audio stoped with type %d' % message_type)