summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/android/audio.py
blob: 2115f198aedd76304c1212b7cb7dc4528c78e98d (plain) (blame)
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
from jnius import autoclass

from plyer.facades.audio import Audio

# Recorder Classes
MediaRecorder = autoclass('android.media.MediaRecorder')
AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')

# Player Classes
MediaPlayer = autoclass('android.media.MediaPlayer')


class AndroidAudio(Audio):
    '''Audio for android.

    For recording audio we use MediaRecorder Android class.
    For playing audio we use MediaPlayer Android class.
    '''

    def __init__(self, file_path=None):
        default_path = '/sdcard/testrecorder.3gp'
        super(AndroidAudio, self).__init__(file_path or default_path)

        self._recorder = None
        self._player = None

    def _start(self):
        self._recorder = MediaRecorder()
        self._recorder.setAudioSource(AudioSource.DEFAULT)
        self._recorder.setOutputFormat(OutputFormat.DEFAULT)
        self._recorder.setAudioEncoder(AudioEncoder.DEFAULT)
        self._recorder.setOutputFile(self.file_path)

        self._recorder.prepare()
        self._recorder.start()

    def _stop(self):
        if self._recorder:
            self._recorder.stop()
            self._recorder.release()
            self._recorder = None

        if self._player:
            self._player.stop()
            self._player.release()
            self._player = None

    def _play(self):
        self._player = MediaPlayer()
        self._player.setDataSource(self.file_path)
        self._player.prepare()
        self._player.start()


def instance():
    return AndroidAudio()