recording_test
This commit is contained in:
47
lib/platform/audio_recorder_io.dart
Normal file
47
lib/platform/audio_recorder_io.dart
Normal file
@ -0,0 +1,47 @@
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:record/record.dart';
|
||||
|
||||
mixin AudioRecorderMixin {
|
||||
Future<void> recordFile(AudioRecorder recorder, RecordConfig config) async {
|
||||
final path = await _getPath();
|
||||
|
||||
await recorder.start(config, path: path);
|
||||
}
|
||||
|
||||
Future<void> recordStream(AudioRecorder recorder, RecordConfig config) async {
|
||||
final path = await _getPath();
|
||||
|
||||
final file = File(path);
|
||||
|
||||
final stream = await recorder.startStream(config);
|
||||
|
||||
stream.listen(
|
||||
(data) {
|
||||
// ignore: avoid_print
|
||||
print(
|
||||
recorder.convertBytesToInt16(Uint8List.fromList(data)),
|
||||
);
|
||||
file.writeAsBytesSync(data, mode: FileMode.append);
|
||||
},
|
||||
// ignore: avoid_print
|
||||
onDone: () {
|
||||
// ignore: avoid_print
|
||||
print('End of stream. File written to $path.');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void downloadWebData(String path) {}
|
||||
|
||||
Future<String> _getPath() async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
return p.join(
|
||||
dir.path,
|
||||
'audio_${DateTime.now().millisecondsSinceEpoch}.m4a',
|
||||
);
|
||||
}
|
||||
}
|
1
lib/platform/audio_recorder_platform.dart
Normal file
1
lib/platform/audio_recorder_platform.dart
Normal file
@ -0,0 +1 @@
|
||||
export 'audio_recorder_web.dart' if (dart.library.io) 'audio_recorder_io.dart';
|
33
lib/platform/audio_recorder_web.dart
Normal file
33
lib/platform/audio_recorder_web.dart
Normal file
@ -0,0 +1,33 @@
|
||||
// ignore_for_file: avoid_web_libraries_in_flutter
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:record/record.dart';
|
||||
|
||||
mixin AudioRecorderMixin {
|
||||
Future<void> recordFile(AudioRecorder recorder, RecordConfig config) {
|
||||
return recorder.start(config, path: '');
|
||||
}
|
||||
|
||||
Future<void> recordStream(AudioRecorder recorder, RecordConfig config) async {
|
||||
final b = <Uint8List>[];
|
||||
final stream = await recorder.startStream(config);
|
||||
|
||||
stream.listen(
|
||||
(data) => b.add(data),
|
||||
onDone: () => downloadWebData(html.Url.createObjectUrl(html.Blob(b))),
|
||||
);
|
||||
}
|
||||
|
||||
void downloadWebData(String path) {
|
||||
// Simple download code for web testing
|
||||
final anchor = html.document.createElement('a') as html.AnchorElement
|
||||
..href = path
|
||||
..style.display = 'none'
|
||||
..download = 'audio.wav';
|
||||
html.document.body!.children.add(anchor);
|
||||
anchor.click();
|
||||
html.document.body!.children.remove(anchor);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user