poem_voice_widget.dart
7.66 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_plugin_record/flutter_plugin_record.dart';
import 'package:flutter_plugin_record/utils/common_toast.dart';
import 'custom_overlay.dart';
typedef StartRecord = Future Function();
typedef StopRecord = Future Function();
class PoemVoiceWidget extends StatefulWidget {
final Function? startRecord;
final Function? stopRecord;
final double? height;
final EdgeInsets? margin;
final Decoration? decoration;
/// startRecord 开始录制回调 stopRecord回调
const PoemVoiceWidget(
{Key? key,
this.startRecord,
this.stopRecord,
this.height,
this.decoration,
this.margin})
: super(key: key);
@override
_PoemVoiceWidgetState createState() => _PoemVoiceWidgetState();
}
class _PoemVoiceWidgetState extends State<PoemVoiceWidget> {
// 倒计时总时长
final int _countTotal = 12;
double startY = 0.0;
double offset = 0.0;
bool isUp = false;
String textShow = "按住说话";
String toastShow = "手指上滑,取消发送";
String voiceIco = "images/voice_volume_1.png";
///默认隐藏状态
bool voiceState = true;
FlutterPluginRecord? recordPlugin;
Timer? _timer;
int _count = 0;
OverlayEntry? overlayEntry;
String audioFilePath = "";
@override
void initState() {
super.initState();
recordPlugin = FlutterPluginRecord();
_init();
///初始化方法的监听
recordPlugin?.responseFromInit.listen((data) {
// if (data) {
// print("初始化成功");
// } else {
// print("初始化失败");
// }
});
/// 开始录制或结束录制的监听
recordPlugin?.response.listen((data) {
if (data.msg == "onStop") {
///结束录制时会返回录制文件的地址方便上传服务器
if (widget.stopRecord != null) {
audioFilePath = data.path!;
widget.stopRecord!(data.path, data.audioTimeLength);
}
} else if (data.msg == "onStart") {
if (widget.startRecord != null) widget.startRecord!();
}
});
///录制过程监听录制的声音的大小 方便做语音动画显示图片的样式
recordPlugin!.responseFromAmplitude.listen((data) {
var voiceData = double.parse(data.msg ?? '');
setState(() {
if (voiceData > 0 && voiceData < 0.1) {
voiceIco = "images/voice_volume_2.png";
} else if (voiceData > 0.2 && voiceData < 0.3) {
voiceIco = "images/voice_volume_3.png";
} else if (voiceData > 0.3 && voiceData < 0.4) {
voiceIco = "images/voice_volume_4.png";
} else if (voiceData > 0.4 && voiceData < 0.5) {
voiceIco = "images/voice_volume_5.png";
} else if (voiceData > 0.5 && voiceData < 0.6) {
voiceIco = "images/voice_volume_6.png";
} else if (voiceData > 0.6 && voiceData < 0.7) {
voiceIco = "images/voice_volume_7.png";
} else if (voiceData > 0.7 && voiceData < 1) {
voiceIco = "images/voice_volume_7.png";
} else {
voiceIco = "images/voice_volume_1.png";
}
if (overlayEntry != null) {
overlayEntry!.markNeedsBuild();
}
});
});
}
///显示录音悬浮布局
buildOverLayView(BuildContext context) {
if (overlayEntry == null) {
overlayEntry = OverlayEntry(builder: (content) {
return CustomOverlay(
icon: Column(
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 10),
child: _countTotal - _count < 11
? Center(
child: Padding(
padding: const EdgeInsets.only(bottom: 15.0),
child: Text(
(_countTotal - _count).toString(),
style: const TextStyle(
fontSize: 70.0,
color: Colors.white,
),
),
),
)
: Image.asset(
voiceIco,
width: 100,
height: 100,
package: 'flutter_plugin_record',
),
),
Text(
toastShow,
style: const TextStyle(
fontStyle: FontStyle.normal,
color: Colors.white,
fontSize: 14,
),
)
],
),
);
});
Overlay.of(context)!.insert(overlayEntry!);
}
}
showVoiceView() {
setState(() {
textShow = "松开结束";
voiceState = false;
});
///显示录音悬浮布局
buildOverLayView(context);
start();
}
hideVoiceView() {
if (_timer!.isActive) {
if (_count < 1) {
CommonToast.showView(
context: context,
msg: '说话时间太短',
icon: const Text(
'!',
style: TextStyle(fontSize: 80, color: Colors.white),
));
isUp = true;
}
_timer?.cancel();
_count = 0;
}
setState(() {
textShow = "按住说话";
voiceState = true;
});
stop();
if (overlayEntry != null) {
overlayEntry?.remove();
overlayEntry = null;
}
// if (isUp) {
// print("取消发送");
// } else {
// print("进行发送");
// }
}
moveVoiceView() {
setState(() {
isUp = startY - offset > 100 ? true : false;
if (isUp) {
textShow = "松开手指,取消发送";
toastShow = textShow;
} else {
textShow = "松开结束";
toastShow = "手指上滑,取消发送";
}
});
}
///初始化语音录制的方法
void _init() async {
recordPlugin?.initRecordMp3();
}
///开始语音录制的方法
void start() async {
recordPlugin?.start();
}
///停止语音录制的方法
void stop() {
recordPlugin?.stop();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onLongPressStart: (details) {
startY = details.globalPosition.dy;
_timer = Timer.periodic(const Duration(milliseconds: 1000), (t) {
_count++;
if (_count == _countTotal) {
hideVoiceView();
}
});
showVoiceView();
},
onLongPressEnd: (details) {
hideVoiceView();
},
onLongPressMoveUpdate: (details) {
offset = details.globalPosition.dy;
moveVoiceView();
},
child: Container(
height: widget.height ?? 60,
margin: widget.margin ?? const EdgeInsets.fromLTRB(50, 0, 50, 20),
child: Icon(
Icons.mic_none,
size: 70.0,
color: Colors.black45.withOpacity(0.6),
),
),
),
IconButton(
icon: const Icon(
Icons.play_circle_outline,
size: 28.0,
),
onPressed: () {
print("######:" + audioFilePath);
recordPlugin!.playByPath(audioFilePath, "file");
},
),
],
),
);
}
@override
void dispose() {
recordPlugin?.dispose();
_timer?.cancel();
super.dispose();
}
}