File: audio.py

package info (click to toggle)
python-plyer 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 13,395; sh: 217; makefile: 177
file content (103 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'''
Audio
=====

The :class:`Audio` is used for recording audio.

Default path for recording is set in platform implementation.

.. note::
        On Android the `RECORD_AUDIO`, `WAKE_LOCK` permissions are needed.

Simple Examples
---------------

To get the file path::

    >>> audio.file_path
    '/sdcard/testrecorder.3gp'

To set the file path::

    >>> import os
    >>> current_list = os.listdir('.')
    ['/sdcard/testrecorder.3gp', '/sdcard/testrecorder1.3gp',
    '/sdcard/testrecorder2.3gp', '/sdcard/testrecorder3.3gp']
    >>> file_path = current_list[2]
    >>> audio.file_path = file_path

To start recording::

    >>> from plyer import audio
    >>> audio.start()

To stop recording::

    >>> audio.stop()

To play recording::

    >>> audio.play()

Supported Platforms
-------------------
Android, Windows, macOS

'''


class Audio:
    '''
    Audio facade.
    '''

    state = 'ready'
    _file_path = ''

    def __init__(self, file_path=None):
        super().__init__()
        self._file_path = file_path or self._file_path

    def start(self):
        '''
        Start record.
        '''
        self._start()
        self.state = 'recording'

    def stop(self):
        '''
        Stop record.
        '''
        self._stop()
        self.state = 'ready'

    def play(self):
        '''
        Play current recording.
        '''
        self._play()
        self.state = 'playing'

    @property
    def file_path(self):
        return self._file_path

    @file_path.setter
    def file_path(self, location):
        '''
        Location of the recording.
        '''
        assert isinstance(location, str), 'Location must be string or unicode'
        self._file_path = location

    # private

    def _start(self):
        raise NotImplementedError()

    def _stop(self):
        raise NotImplementedError()

    def _play(self):
        raise NotImplementedError()