record 5.0.0-beta.3+1
record: ^5.0.0-beta.3+1 copied to clipboard
Audio recorder from microphone to a given file path with multiple codecs, bit rate and sampling rate options.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:record/record.dart';
import 'package:record_example/audio_player.dart';
// import 'dart:html' as html;
void main() => runApp(const MyApp());
class _AudioRecorder extends StatefulWidget {
final void Function(String path) onStop;
const _AudioRecorder({Key? key, required this.onStop}) : super(key: key);
@override
State<_AudioRecorder> createState() => _AudioRecorderState();
}
class _AudioRecorderState extends State<_AudioRecorder> {
int _recordDuration = 0;
Timer? _timer;
late final AudioRecorder _audioRecorder;
StreamSubscription<RecordState>? _recordSub;
RecordState _recordState = RecordState.stop;
StreamSubscription<Amplitude>? _amplitudeSub;
Amplitude? _amplitude;
@override
void initState() {
_audioRecorder = AudioRecorder();
_recordSub = _audioRecorder.onStateChanged().listen((recordState) {
_updateRecordState(recordState);
});
_amplitudeSub = _audioRecorder
.onAmplitudeChanged(const Duration(milliseconds: 300))
.listen((amp) {
setState(() => _amplitude = amp);
});
super.initState();
}
Future<void> _start() async {
try {
if (await _audioRecorder.hasPermission()) {
const encoder = AudioEncoder.aacLc;
// We don't do anything with this but printing
final isSupported = await _audioRecorder.isEncoderSupported(
encoder,
);
debugPrint('${encoder.name} supported: $isSupported');
final devs = await _audioRecorder.listInputDevices();
debugPrint(devs.toString());
const config = RecordConfig(encoder: encoder, numChannels: 1);
// Record to file
String path;
if (kIsWeb) {
path = '';
} else {
final dir = await getApplicationDocumentsDirectory();
path = p.join(
dir.path,
'audio_${DateTime.now().millisecondsSinceEpoch}.m4a',
);
}
await _audioRecorder.start(config, path: path);
// Record to stream
// final file = File(path);
// final stream = await _audioRecorder.startStream(config);
// stream.listen(
// (data) {
// // ignore: avoid_print
// print(
// _audioRecorder.convertBytesToInt16(Uint8List.fromList(data)),
// );
// file.writeAsBytesSync(data, mode: FileMode.append);
// },
// // ignore: avoid_print
// onDone: () => print('End of stream'),
// );
// Record to stream web
// var b = <Uint8List>[];
// final stream = await _audioRecorder.startStream(config);
// stream.listen(
// (data) {
// b.add(data);
// },
// onDone: () {
// _downloadWebData(html.Url.createObjectUrl(html.Blob(b)));
// },
// );
_recordDuration = 0;
_startTimer();
}
} catch (e) {
if (kDebugMode) {
print(e);
}
}
}
Future<void> _stop() async {
final path = await _audioRecorder.stop();
if (path != null) {
widget.onStop(path);
// _downloadWebData(path);
}
}
// 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);
// html.Url.revokeObjectUrl(path);
// }
Future<void> _pause() => _audioRecorder.pause();
Future<void> _resume() => _audioRecorder.resume();
void _updateRecordState(RecordState recordState) {
setState(() => _recordState = recordState);
switch (recordState) {
case RecordState.pause:
_timer?.cancel();
break;
case RecordState.record:
_startTimer();
break;
case RecordState.stop:
_timer?.cancel();
_recordDuration = 0;
break;
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildRecordStopControl(),
const SizedBox(width: 20),
_buildPauseResumeControl(),
const SizedBox(width: 20),
_buildText(),
],
),
if (_amplitude != null) ...[
const SizedBox(height: 40),
Text('Current: ${_amplitude?.current ?? 0.0}'),
Text('Max: ${_amplitude?.max ?? 0.0}'),
],
],
),
),
);
}
@override
void dispose() {
_timer?.cancel();
_recordSub?.cancel();
_amplitudeSub?.cancel();
_audioRecorder.dispose();
super.dispose();
}
Widget _buildRecordStopControl() {
late Icon icon;
late Color color;
if (_recordState != RecordState.stop) {
icon = const Icon(Icons.stop, color: Colors.red, size: 30);
color = Colors.red.withOpacity(0.1);
} else {
final theme = Theme.of(context);
icon = Icon(Icons.mic, color: theme.primaryColor, size: 30);
color = theme.primaryColor.withOpacity(0.1);
}
return ClipOval(
child: Material(
color: color,
child: InkWell(
child: SizedBox(width: 56, height: 56, child: icon),
onTap: () {
(_recordState != RecordState.stop) ? _stop() : _start();
},
),
),
);
}
Widget _buildPauseResumeControl() {
if (_recordState == RecordState.stop) {
return const SizedBox.shrink();
}
late Icon icon;
late Color color;
if (_recordState == RecordState.record) {
icon = const Icon(Icons.pause, color: Colors.red, size: 30);
color = Colors.red.withOpacity(0.1);
} else {
final theme = Theme.of(context);
icon = const Icon(Icons.play_arrow, color: Colors.red, size: 30);
color = theme.primaryColor.withOpacity(0.1);
}
return ClipOval(
child: Material(
color: color,
child: InkWell(
child: SizedBox(width: 56, height: 56, child: icon),
onTap: () {
(_recordState == RecordState.pause) ? _resume() : _pause();
},
),
),
);
}
Widget _buildText() {
if (_recordState != RecordState.stop) {
return _buildTimer();
}
return const Text("Waiting to record");
}
Widget _buildTimer() {
final String minutes = _formatNumber(_recordDuration ~/ 60);
final String seconds = _formatNumber(_recordDuration % 60);
return Text(
'$minutes : $seconds',
style: const TextStyle(color: Colors.red),
);
}
String _formatNumber(int number) {
String numberStr = number.toString();
if (number < 10) {
numberStr = '0$numberStr';
}
return numberStr;
}
void _startTimer() {
_timer?.cancel();
_timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
setState(() => _recordDuration++);
});
}
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool showPlayer = false;
String? audioPath;
@override
void initState() {
showPlayer = false;
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: showPlayer
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 25),
child: AudioPlayer(
source: audioPath!,
onDelete: () {
setState(() => showPlayer = false);
},
),
)
: _AudioRecorder(
onStop: (path) {
if (kDebugMode) print('Recorded file path: $path');
setState(() {
audioPath = path;
showPlayer = true;
});
},
),
),
),
);
}
}