Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/play_audio.py
blob: c06a66367b4c837463262f53243be33dccae2b64 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
 aplay.py
 refactored based on Jukebox Activity
 Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
 Copyright (C) 2007 Red Hat, Inc.
 Copyright (C) 2008-2010 Kushal Das <kushal@fedoraproject.org>
 Copyright (C) 2010-11 Walter Bender
"""

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import os
import subprocess

import logging
_logger = logging.getLogger('lettermatch-activity')


GST_PATHS = ['/usr/bin/gst-launch', '/bin/gst-launch',
             '/usr/local/bin/gst-launch',
             '/usr/bin/gst-launch-1.0', '/bin/gst-launch-1.0',
             '/usr/local/bin/gst-launch-1.0',
             '/usr/bin/gst-launch-0.10', '/bin/gst-launch-0.10',
             '/usr/local/bin/gst-launch-0.10']


def play_audio_from_file(file_path):
    """ Audio media """

    if not hasattr(play_audio_from_file, 'gst_launch'):
        for path in GST_PATHS:
            if os.path.exists(path):
                play_audio_from_file.gst_launch = path
                _logger.debug(path)
                break

    if not hasattr(play_audio_from_file, 'gst_launch'):
        _logger.debug('gst-launch not found')
        return

    command_line = [play_audio_from_file.gst_launch, 'filesrc',
                    'location=' + file_path, '! oggdemux', '! vorbisdec',
                    '! audioconvert', '! alsasink']
    check_output(command_line, 'unable to play audio file %s' % (file_path))


def check_output(command, warning):
    ''' Workaround for old systems without subprocess.check_output'''
    if hasattr(subprocess, 'check_output'):
        try:
            output = subprocess.check_output(command)
        except subprocess.CalledProcessError:
            log.warning(warning)
            return None
    else:
        import commands

        cmd = ''
        for c in command:
            cmd += c
            cmd += ' '
        (status, output) = commands.getstatusoutput(cmd)
        if status != 0:
            _logger.warning(warning)
            return None
    return output