| 1 |
''' |
| 2 |
Audio |
| 3 |
===== |
| 4 |
|
| 5 |
The :class:`Audio` is used for recording audio. |
| 6 |
|
| 7 |
Default path for recording is set in platform implementation. |
| 8 |
|
| 9 |
.. note:: |
| 10 |
On Android the `RECORD_AUDIO`, `WAKE_LOCK` permissions are needed. |
| 11 |
|
| 12 |
Simple Examples |
| 13 |
--------------- |
| 14 |
|
| 15 |
To get the file path:: |
| 16 |
|
| 17 |
>>> audio.file_path |
| 18 |
'/sdcard/testrecorder.3gp' |
| 19 |
|
| 20 |
To set the file path:: |
| 21 |
|
| 22 |
>>> import os |
| 23 |
>>> current_list = os.listdir('.') |
| 24 |
['/sdcard/testrecorder.3gp', '/sdcard/testrecorder1.3gp', |
| 25 |
'/sdcard/testrecorder2.3gp', '/sdcard/testrecorder3.3gp'] |
| 26 |
>>> file_path = current_list[2] |
| 27 |
>>> audio.file_path = file_path |
| 28 |
|
| 29 |
To start recording:: |
| 30 |
|
| 31 |
>>> from plyer import audio |
| 32 |
>>> audio.start() |
| 33 |
|
| 34 |
To stop recording:: |
| 35 |
|
| 36 |
>>> audio.stop() |
| 37 |
|
| 38 |
To play recording:: |
| 39 |
|
| 40 |
>>> audio.play() |
| 41 |
|
| 42 |
Supported Platforms |
| 43 |
------------------- |
| 44 |
Android, Windows, macOS |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
|
| 49 |
class Audio: |
| 50 |
''' |
| 51 |
Audio facade. |
| 52 |
''' |
| 53 |
|
| 54 |
state = 'ready' |
| 55 |
_file_path = '' |
| 56 |
|
| 57 |
def __init__(self, file_path=None): |
| 58 |
super().__init__() |
| 59 |
self._file_path = file_path or self._file_path |
| 60 |
|
| 61 |
def start(self): |
| 62 |
''' |
| 63 |
Start record. |
| 64 |
''' |
| 65 |
self._start() |
| 66 |
self.state = 'recording' |
| 67 |
|
| 68 |
def stop(self): |
| 69 |
''' |
| 70 |
Stop record. |
| 71 |
''' |
| 72 |
self._stop() |
| 73 |
self.state = 'ready' |
| 74 |
|
| 75 |
def play(self): |
| 76 |
''' |
| 77 |
Play current recording. |
| 78 |
''' |
| 79 |
self._play() |
| 80 |
self.state = 'playing' |
| 81 |
|
| 82 |
@property |
| 83 |
def file_path(self): |
| 84 |
return self._file_path |
| 85 |
|
| 86 |
@file_path.setter |
| 87 |
def file_path(self, location): |
| 88 |
''' |
| 89 |
Location of the recording. |
| 90 |
''' |
| 91 |
assert isinstance(location, str), 'Location must be string or unicode' |
| 92 |
self._file_path = location |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def _start(self): |
| 97 |
raise NotImplementedError() |
| 98 |
|
| 99 |
def _stop(self): |
| 100 |
raise NotImplementedError() |
| 101 |
|
| 102 |
def _play(self): |
| 103 |
raise NotImplementedError() |