-
Notifications
You must be signed in to change notification settings - Fork 16
/
recorder_view.dart
148 lines (125 loc) · 3.62 KB
/
recorder_view.dart
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
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_audio_recorder2/flutter_audio_recorder2.dart';
class RecorderView extends StatefulWidget {
final Function onSaved;
const RecorderView({Key? key, required this.onSaved}) : super(key: key);
@override
_RecorderViewState createState() => _RecorderViewState();
}
enum RecordingState {
UnSet,
Set,
Recording,
Stopped,
}
class _RecorderViewState extends State<RecorderView> {
IconData _recordIcon = Icons.mic_none;
String _recordText = 'Click To Start';
RecordingState _recordingState = RecordingState.UnSet;
// Recorder properties
late FlutterAudioRecorder2 audioRecorder;
@override
void initState() {
super.initState();
FlutterAudioRecorder2.hasPermissions.then((hasPermision) {
if (hasPermision!) {
_recordingState = RecordingState.Set;
_recordIcon = Icons.mic;
_recordText = 'Record';
}
});
}
@override
void dispose() {
_recordingState = RecordingState.UnSet;
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
MaterialButton(
onPressed: () async {
await _onRecordButtonPressed();
setState(() {});
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
child: Container(
width: 150,
height: 150,
child: Icon(
_recordIcon,
size: 50,
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
child: Text(_recordText),
padding: const EdgeInsets.all(8),
))
],
);
}
Future<void> _onRecordButtonPressed() async {
switch (_recordingState) {
case RecordingState.Set:
await _recordVoice();
break;
case RecordingState.Recording:
await _stopRecording();
_recordingState = RecordingState.Stopped;
_recordIcon = Icons.fiber_manual_record;
_recordText = 'Record new one';
break;
case RecordingState.Stopped:
await _recordVoice();
break;
case RecordingState.UnSet:
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Please allow recording from settings.'),
));
break;
}
}
_initRecorder() async {
Directory appDirectory = await getApplicationDocumentsDirectory();
String filePath = appDirectory.path +
'/' +
DateTime.now().millisecondsSinceEpoch.toString() +
'.aac';
audioRecorder =
FlutterAudioRecorder2(filePath, audioFormat: AudioFormat.AAC);
await audioRecorder.initialized;
}
_startRecording() async {
await audioRecorder.start();
// await audioRecorder.current(channel: 0);
}
_stopRecording() async {
await audioRecorder.stop();
widget.onSaved();
}
Future<void> _recordVoice() async {
final hasPermission = await FlutterAudioRecorder2.hasPermissions;
if (hasPermission ?? false) {
await _initRecorder();
await _startRecording();
_recordingState = RecordingState.Recording;
_recordIcon = Icons.stop;
_recordText = 'Recording';
} else {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Please allow recording from settings.'),
));
}
}
}