comparison src/cagou/plugins/plugin_upload_voice.py @ 95:3e3c097b07b7

upload: added voice plugin (for Android)
author Goffi <goffi@goffi.org>
date Tue, 27 Dec 2016 21:54:09 +0100
parents
children 641678ddc26c
comparison
equal deleted inserted replaced
94:b94967b7ebfd 95:3e3c097b07b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 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 if sys.platform == "android":
29 from kivy import properties
30 from plyer import audio
31
32
33 PLUGIN_INFO = {
34 "name": _(u"voice"),
35 "main": "VoiceRecorder",
36 "platforms": ["android"],
37 "description": _(u"upload a voice record"),
38 }
39
40
41 class VoiceRecorder(BoxLayout):
42 callback = properties.ObjectProperty()
43 cancel_cb = properties.ObjectProperty()
44 recording = properties.BooleanProperty(False)
45 playing = properties.BooleanProperty(False)
46 time = properties.NumericProperty(0)
47
48 def __init__(self, **kwargs):
49 super(VoiceRecorder, self).__init__(**kwargs)
50 self._started_at = None
51 self._counter_timer = None
52 self._play_timer = None
53 self.record_time = None
54 self.audio = audio
55 self.audio.file_path = "/sdcard/cagou_record.3gp"
56
57 def _updateTimer(self, dt):
58 self.time = int(time.time() - self._started_at)
59
60 def switchRecording(self):
61 if self.playing:
62 self._stopPlaying()
63 if self.recording:
64 try:
65 audio.stop()
66 except Exception as e:
67 # an exception can happen if record is pressed
68 # repeatedly in a short time (not a normal use)
69 log.warning(u"Exception on stop: {}".format(e))
70 self._counter_timer.cancel()
71 self.time = self.time + 1
72 else:
73 audio.start()
74 self._started_at = time.time()
75 self.time = 0
76 self._counter_timer = Clock.schedule_interval(self._updateTimer, 1)
77
78 self.recording = not self.recording
79
80 def _stopPlaying(self, dummy=None):
81 if self.record_time is None:
82 log.error("_stopPlaying should no be called when record_time is None")
83 return
84 audio.stop()
85 self.playing = False
86 self.time = self.record_time
87 if self._counter_timer is not None:
88 self._counter_timer.cancel()
89
90 def playRecord(self):
91 if self.recording:
92 return
93 if self.playing:
94 self._stopPlaying()
95 else:
96 try:
97 audio.play()
98 except Exception as e:
99 # an exception can happen in the same situation
100 # as for audio.stop() above (i.e. bad record)
101 log.warning(u"Exception on play: {}".format(e))
102 self.time = 0
103 return
104
105 self.playing = True
106 self.record_time = self.time
107 Clock.schedule_once(self._stopPlaying, self.time + 1)
108 self._started_at = time.time()
109 self.time = 0
110 self._counter_timer = Clock.schedule_interval(self._updateTimer, 0.5)