Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 44 additions & 32 deletions lib/components/recordingSession/recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Recorder extends StatefulWidget {

class _RecorderState extends State<Recorder> {
bool _isRecording = false;
bool _recording = false;
Stopwatch stopwatch = new Stopwatch();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the new keyword

StreamSubscription<dynamic> _recorderSubscription;
FlutterSoundRecorder recorderModule;
String _recorderTxt = '00:00:00';
Expand Down Expand Up @@ -156,7 +158,6 @@ class _RecorderState extends State<Recorder> {
await recorderModule.stopRecorder();
final DateTime exportedTime = getExportedTime();
ws.sendAudioFile(savePath, userId, topic, exportedTime);

await cancelRecorderSubscriptions();
await closeAudioSession();
} catch (err) {
Expand All @@ -172,6 +173,44 @@ class _RecorderState extends State<Recorder> {
prefs.setString('session', value);
}

Future<void> recorderPressed() async {
if (!_isRecording) {
await showDialog(
context: context,
builder: (BuildContext context) => MyTopicDialog(
onTopicChanged: (String childTopic) {
topic = childTopic;
},
),
);
setState(() {
_recording = true;
stopwatch.reset();
stopwatch.start();
});
while (_recording) {
await startRecorder();
await Future.delayed(
const Duration(seconds: 5), () async => await stopRecorder());
}
} else {
if (_isRecording) {
setState(() {
_recording = false;
stopwatch.stop();
});
stopRecorder();
}
}
}

DateTime getExportedTime() {
final DateTime now = DateTime.now();
final DateTime date = DateTime(
now.year, now.month, now.day, now.hour, now.minute, now.second);
return date;
}

@override
Widget build(BuildContext context) {
return Expanded(
Expand All @@ -180,22 +219,8 @@ class _RecorderState extends State<Recorder> {
children: <Widget>[
FloatingActionButton(
heroTag: 'recorder',
onPressed: () async {
if (!_isRecording) {
await showDialog(
context: context,
builder: (BuildContext context) => MyTopicDialog(
onTopicChanged: (String childTopic) {
topic = childTopic;
},
),
);
return startRecorder();
} else {
stopRecorder();
}
},
child: _isRecording
onPressed: () async => await recorderPressed(),
child: _recording
? Icon(Icons.stop)
: Icon(
Icons.mic,
Expand All @@ -205,15 +230,15 @@ class _RecorderState extends State<Recorder> {
),
Container(
child: AutoSizeText(
_recorderTxt,
stopwatch.elapsed.toString().substring(2, 10),
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
),
),
Container(
child: _isRecording
child: _recording
? LinearProgressIndicator(
value: 100.0 / 160.0 * (_dbLevel ?? 1) / 100,
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
Expand All @@ -225,17 +250,4 @@ class _RecorderState extends State<Recorder> {
),
);
}

DateTime getExportedTime() {
final DateTime now = DateTime.now();
final DateTime date = DateTime(
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second
);
return date;
}
}
13 changes: 4 additions & 9 deletions lib/components/recordingSession/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ class MyWebSocket {
Future<void> sendAudioFile(
String exportedAudioData, int userID, String topic, DateTime date) async {
final List<int> audioData = await processAudioFile(exportedAudioData);
final IncomingAudioEvent incomingAudioEvent = IncomingAudioEvent(
audioData,
userID,
topic,
date.toString()
);
print(audioData.toString() + '\n'); // array of integers/bytes
final IncomingAudioEvent incomingAudioEvent =
IncomingAudioEvent(audioData, userID, topic, date.toString());
channel.sink.add(
json.encode(
incomingAudioEvent.toJson()
),
json.encode(incomingAudioEvent.toJson()),
);
}

Expand Down