audio_recorder_nullsafety 1.0.3 audio_recorder_nullsafety: ^1.0.3 copied to clipboard
A Flutter plugin to record audio.
import 'dart:async';
import 'dart:io' as io;
import 'dart:math';
import 'package:audio_recorder_nullsafety/audio_recorder_nullsafety.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin audio recorder'),
),
body: const AppBody(),
),
);
}
}
class AppBody extends StatefulWidget {
final LocalFileSystem localFileSystem;
const AppBody({this.localFileSystem = const LocalFileSystem(), Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => AppBodyState();
}
class AppBodyState extends State<AppBody> {
Recording _recording = Recording();
bool? _isRecording = false;
Random random = Random();
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[
TextButton(
onPressed: _isRecording! ? null : _start,
child: const Text("Start"),
),
TextButton(
onPressed: _isRecording! ? _stop : null,
child: const Text("Stop"),
),
TextField(
controller: _controller,
decoration: const InputDecoration(
hintText: 'Enter a custom path',
),
),
Text("File path of the record: ${_recording.path}"),
Text("Format: ${_recording.audioOutputFormat}"),
Text("Extension : ${_recording.extension}"),
Text("Audio recording duration : ${_recording.duration.toString()}")
]),
),
);
}
Future<void> _start() async {
try {
if (await (AudioRecorder.hasPermissions as FutureOr<bool>)) {
if (_controller.text != "") {
String path = _controller.text;
if (!_controller.text.contains('/')) {
final io.Directory appDocDirectory = await getApplicationDocumentsDirectory();
path = '${appDocDirectory.path}/${_controller.text}';
}
print("Start recording: $path");
await AudioRecorder.start(path: path, audioOutputFormat: AudioOutputFormat.AAC);
} else {
await AudioRecorder.start();
}
final bool? isRecording = await AudioRecorder.isRecording;
setState(() {
_recording = Recording(duration: const Duration(), path: "");
_isRecording = isRecording;
});
} else {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("You must accept permissions")));
}
} catch (e) {
print(e);
}
}
Future<void> _stop() async {
final recording = await AudioRecorder.stop();
print("Stop recording: ${recording.path}");
final bool? isRecording = await AudioRecorder.isRecording;
final File file = widget.localFileSystem.file(recording.path);
print(" File length: ${await file.length()}");
setState(() {
_recording = recording;
_isRecording = isRecording;
});
_controller.text = recording.path!;
}
}