comparison cagou/plugins/plugin_transfer_voice.py @ 126:cd99f70ea592

global file reorganisation: - follow common convention by puttin cagou in "cagou" instead of "src/cagou" - added VERSION in cagou with current version - updated dates - moved main executable in /bin - moved buildozer files in root directory - temporary moved platform to assets/platform
author Goffi <goffi@goffi.org>
date Thu, 05 Apr 2018 17:11:21 +0200
parents src/cagou/plugins/plugin_transfer_voice.py@5d2289127bb7
children 1b835bcfa663
comparison
equal deleted inserted replaced
125:b6e6afb0dc46 126:cd99f70ea592
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016-2018 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 sat.core import log as logging
22 log = logging.getLogger(__name__)
23 from sat.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": _(u"voice"),
35 "main": "VoiceRecorder",
36 "platforms": ["android"],
37 "description": _(u"transmit a voice record"),
38 "icon_medium": u"{media}/icons/muchoslava/png/micro_off_50.png",
39 }
40
41
42 class VoiceRecorder(BoxLayout):
43 callback = properties.ObjectProperty()
44 cancel_cb = properties.ObjectProperty()
45 recording = properties.BooleanProperty(False)
46 playing = properties.BooleanProperty(False)
47 time = properties.NumericProperty(0)
48
49 def __init__(self, **kwargs):
50 super(VoiceRecorder, self).__init__(**kwargs)
51 self._started_at = None
52 self._counter_timer = None
53 self._play_timer = None
54 self.record_time = None
55 self.audio = audio
56 self.audio.file_path = "/sdcard/cagou_record.3gp"
57
58 def _updateTimer(self, dt):
59 self.time = int(time.time() - self._started_at)
60
61 def switchRecording(self):
62 if self.playing:
63 self._stopPlaying()
64 if self.recording:
65 try:
66 audio.stop()
67 except Exception as e:
68 # an exception can happen if record is pressed
69 # repeatedly in a short time (not a normal use)
70 log.warning(u"Exception on stop: {}".format(e))
71 self._counter_timer.cancel()
72 self.time = self.time + 1
73 else:
74 audio.start()
75 self._started_at = time.time()
76 self.time = 0
77 self._counter_timer = Clock.schedule_interval(self._updateTimer, 1)
78
79 self.recording = not self.recording
80
81 def _stopPlaying(self, dummy=None):
82 if self.record_time is None:
83 log.error("_stopPlaying should no be called when record_time is None")
84 return
85 audio.stop()
86 self.playing = False
87 self.time = self.record_time
88 if self._counter_timer is not None:
89 self._counter_timer.cancel()
90
91 def playRecord(self):
92 if self.recording:
93 return
94 if self.playing:
95 self._stopPlaying()
96 else:
97 try:
98 audio.play()
99 except Exception as e:
100 # an exception can happen in the same situation
101 # as for audio.stop() above (i.e. bad record)
102 log.warning(u"Exception on play: {}".format(e))
103 self.time = 0
104 return
105
106 self.playing = True
107 self.record_time = self.time
108 Clock.schedule_once(self._stopPlaying, self.time + 1)
109 self._started_at = time.time()
110 self.time = 0
111 self._counter_timer = Clock.schedule_interval(self._updateTimer, 0.5)