Mercurial > libervia-desktop-kivy
comparison libervia/desktop_kivy/plugins/plugin_transfer_voice.py @ 493:b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 18:26:16 +0200 |
parents | cagou/plugins/plugin_transfer_voice.py@203755bbe0fe |
children |
comparison
equal
deleted
inserted
replaced
492:5114bbb5daa3 | 493:b3cedbee561d |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 #Libervia Desktop-Kivy | |
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from libervia.backend.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from libervia.backend.core.i18n import _ | |
24 from kivy.uix.boxlayout import BoxLayout | |
25 import sys | |
26 import time | |
27 from kivy.clock import Clock | |
28 from kivy import properties | |
29 if sys.platform == "android": | |
30 from plyer import audio | |
31 | |
32 | |
33 PLUGIN_INFO = { | |
34 "name": _("voice"), | |
35 "main": "VoiceRecorder", | |
36 "platforms": ["android"], | |
37 "description": _("transmit a voice record"), | |
38 "icon_medium": "{media}/icons/muchoslava/png/micro_off_50.png", | |
39 "android_permissons": ["RECORD_AUDIO"], | |
40 } | |
41 | |
42 | |
43 class VoiceRecorder(BoxLayout): | |
44 callback = properties.ObjectProperty() | |
45 cancel_cb = properties.ObjectProperty() | |
46 recording = properties.BooleanProperty(False) | |
47 playing = properties.BooleanProperty(False) | |
48 time = properties.NumericProperty(0) | |
49 | |
50 def __init__(self, **kwargs): | |
51 super(VoiceRecorder, self).__init__(**kwargs) | |
52 self._started_at = None | |
53 self._counter_timer = None | |
54 self._play_timer = None | |
55 self.record_time = None | |
56 self.audio = audio | |
57 self.audio.file_path = "/sdcard/cagou_record.3gp" | |
58 | |
59 def _update_timer(self, dt): | |
60 self.time = int(time.time() - self._started_at) | |
61 | |
62 def switch_recording(self): | |
63 if self.playing: | |
64 self._stop_playing() | |
65 if self.recording: | |
66 try: | |
67 audio.stop() | |
68 except Exception as e: | |
69 # an exception can happen if record is pressed | |
70 # repeatedly in a short time (not a normal use) | |
71 log.warning("Exception on stop: {}".format(e)) | |
72 self._counter_timer.cancel() | |
73 self.time = self.time + 1 | |
74 else: | |
75 audio.start() | |
76 self._started_at = time.time() | |
77 self.time = 0 | |
78 self._counter_timer = Clock.schedule_interval(self._update_timer, 1) | |
79 | |
80 self.recording = not self.recording | |
81 | |
82 def _stop_playing(self, __=None): | |
83 if self.record_time is None: | |
84 log.error("_stop_playing should no be called when record_time is None") | |
85 return | |
86 audio.stop() | |
87 self.playing = False | |
88 self.time = self.record_time | |
89 if self._counter_timer is not None: | |
90 self._counter_timer.cancel() | |
91 | |
92 def play_record(self): | |
93 if self.recording: | |
94 return | |
95 if self.playing: | |
96 self._stop_playing() | |
97 else: | |
98 try: | |
99 audio.play() | |
100 except Exception as e: | |
101 # an exception can happen in the same situation | |
102 # as for audio.stop() above (i.e. bad record) | |
103 log.warning("Exception on play: {}".format(e)) | |
104 self.time = 0 | |
105 return | |
106 | |
107 self.playing = True | |
108 self.record_time = self.time | |
109 Clock.schedule_once(self._stop_playing, self.time + 1) | |
110 self._started_at = time.time() | |
111 self.time = 0 | |
112 self._counter_timer = Clock.schedule_interval(self._update_timer, 0.5) |