Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_upload_voice.py @ 96:641678ddc26c
upload (voice): fixed crash on desktop
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 29 Dec 2016 23:47:04 +0100 |
parents | 3e3c097b07b7 |
children |
rev | line source |
---|---|
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import log as logging |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 log = logging.getLogger(__name__) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.i18n import _ |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from kivy.uix.boxlayout import BoxLayout |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 import sys |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from kivy.clock import Clock |
96
641678ddc26c
upload (voice): fixed crash on desktop
Goffi <goffi@goffi.org>
parents:
95
diff
changeset
|
28 from kivy import properties |
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 if sys.platform == "android": |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from plyer import audio |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 PLUGIN_INFO = { |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 "name": _(u"voice"), |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 "main": "VoiceRecorder", |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 "platforms": ["android"], |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 "description": _(u"upload a voice record"), |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 } |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 class VoiceRecorder(BoxLayout): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 callback = properties.ObjectProperty() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 cancel_cb = properties.ObjectProperty() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 recording = properties.BooleanProperty(False) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 playing = properties.BooleanProperty(False) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 time = properties.NumericProperty(0) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 def __init__(self, **kwargs): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 super(VoiceRecorder, self).__init__(**kwargs) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 self._started_at = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self._counter_timer = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self._play_timer = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.record_time = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self.audio = audio |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.audio.file_path = "/sdcard/cagou_record.3gp" |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def _updateTimer(self, dt): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 self.time = int(time.time() - self._started_at) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def switchRecording(self): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 if self.playing: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self._stopPlaying() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 if self.recording: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 try: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 audio.stop() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 except Exception as e: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 # an exception can happen if record is pressed |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 # repeatedly in a short time (not a normal use) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 log.warning(u"Exception on stop: {}".format(e)) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 self._counter_timer.cancel() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 self.time = self.time + 1 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 else: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 audio.start() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self._started_at = time.time() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self._counter_timer = Clock.schedule_interval(self._updateTimer, 1) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 self.recording = not self.recording |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def _stopPlaying(self, dummy=None): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 if self.record_time is None: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 log.error("_stopPlaying should no be called when record_time is None") |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 audio.stop() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 self.playing = False |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 self.time = self.record_time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 if self._counter_timer is not None: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self._counter_timer.cancel() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 def playRecord(self): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 if self.recording: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 if self.playing: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 self._stopPlaying() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 else: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 try: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 audio.play() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 except Exception as e: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 # an exception can happen in the same situation |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 # as for audio.stop() above (i.e. bad record) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 log.warning(u"Exception on play: {}".format(e)) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.playing = True |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.record_time = self.time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 Clock.schedule_once(self._stopPlaying, self.time + 1) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 self._started_at = time.time() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self._counter_timer = Clock.schedule_interval(self._updateTimer, 0.5) |