123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import 'dart:async';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter_audio_recorder/plugins/audio_recorder.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:permission_handler/permission_handler.dart';
- /// 录音模态框
- class RecorderView extends StatefulWidget {
- final Function saveVoice;
- const RecorderView({Key? key, required this.saveVoice}) : super(key: key);
- @override
- _RecorderViewState createState() => _RecorderViewState();
- }
- class _RecorderViewState extends State<RecorderView> {
- IconData _recordIcon = Icons.mic_none;
- MaterialColor colo = Colors.orange;
- /// 录音状态
- RecordingStatus _currentStatus = RecordingStatus.Unset;
- /// 是否录音停止
- bool stop = false;
- Recording? _current;
- // Recorder properties
- late FlutterAudioRecorder? audioRecorder;
- @override
- void initState() {
- super.initState();
- checkPermission();
- }
- // 权限检测
- void checkPermission() async {
- if (await Permission.contacts.request().isGranted) {
- // Either the permission was already granted before or the user just granted it.
- }
- // You can request multiple permissions at once.
- Map<Permission, PermissionStatus> statuses = await [
- Permission.microphone,
- Permission.storage,
- ].request();
- //bool hasPermission = await FlutterAudioRecorder.hasPermissions ?? false;
- if (statuses[Permission.microphone] == PermissionStatus.granted) {
- /// 状态改为已初始化
- _currentStatus = RecordingStatus.Initialized;
- _recordIcon = Icons.mic;
- } else {
- print("权限为获取");
- }
- }
- @override
- void dispose() {
- _currentStatus = RecordingStatus.Unset;
- audioRecorder = null;
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Stack(
- alignment: Alignment.center,
- children: [
- Column(
- children: [
- const SizedBox(
- height: 20,
- ),
- Text(
- (_current == null) ? "0:0:0:0" : _current!.duration.toString(),
- style: const TextStyle(color: Colors.black, fontSize: 20),
- ),
- const SizedBox(
- height: 20,
- ),
- stop == false
- ? ElevatedButton(
- style: ButtonStyle(
- shape: MaterialStateProperty.all(RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10),
- )),
- textStyle: MaterialStateProperty.all(
- const TextStyle(color: Colors.orange))),
- onPressed: () {
- _onRecordButtonPressed();
- setState(() {});
- },
- child: Column(
- children: [
- SizedBox(
- width: 80,
- height: 80,
- child: Icon(
- _recordIcon,
- color: Colors.white,
- size: 80,
- ),
- ),
- const Padding(
- padding: EdgeInsets.all(8.0),
- child: Text(
- "Write Dailry",
- style: TextStyle(color: Colors.white),
- ),
- )
- ],
- ),
- )
- : Padding(
- padding: const EdgeInsets.all(8.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- ElevatedButton(
- style: ButtonStyle(
- textStyle: MaterialStateProperty.all(
- TextStyle(color: colo)),
- shape: MaterialStateProperty.all(
- RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- )),
- onPressed: () async {
- await _onRecordButtonPressed();
- setState(() {});
- },
- child: SizedBox(
- width: 80,
- height: 80,
- child: Icon(
- _recordIcon,
- color: Colors.white,
- size: 50,
- ),
- ),
- ),
- ElevatedButton(
- style: ButtonStyle(
- shape: MaterialStateProperty.all(
- RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- ),
- textStyle: MaterialStateProperty.all(const TextStyle(
- color: Colors.orange,
- ))),
- onPressed: _currentStatus != RecordingStatus.Unset
- ? _stop
- : null,
- child: const SizedBox(
- width: 80,
- height: 80,
- child: Icon(
- Icons.stop,
- color: Colors.white,
- size: 50,
- ),
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ],
- );
- }
- _onRecordButtonPressed() async {
- switch (_currentStatus) {
- case RecordingStatus.Initialized:
- {
- _recordo();
- break;
- }
- case RecordingStatus.Recording:
- {
- _pause();
- break;
- }
- case RecordingStatus.Paused:
- {
- _resume();
- break;
- }
- case RecordingStatus.Stopped:
- {
- _recordo();
- break;
- }
- default:
- print("---------weizb");
- break;
- }
- }
- _initial() async {
- Directory? appDir = await getExternalStorageDirectory();
- String jrecord = 'Audiorecords';
- String dato = "${DateTime.now().millisecondsSinceEpoch.toString()}.wav";
- Directory appDirec = Directory("${appDir!.path}/$jrecord/");
- if (await appDirec.exists()) {
- String patho = "${appDirec.path}$dato";
- audioRecorder = FlutterAudioRecorder(patho, audioFormat: AudioFormat.WAV);
- await audioRecorder!.initialized;
- } else {
- appDirec.create(recursive: true);
- Fluttertoast.showToast(msg: "Start Recording , Press Start");
- String patho = "${appDirec.path}$dato";
- audioRecorder = FlutterAudioRecorder(patho, audioFormat: AudioFormat.WAV);
- await audioRecorder!.initialized;
- }
- }
- _start() async {
- await audioRecorder!.start();
- var recording = await audioRecorder!.current(channel: 0);
- setState(() {
- _current = recording!;
- });
- const tick = Duration(milliseconds: 50);
- Timer.periodic(tick, (Timer t) async {
- if (_currentStatus == RecordingStatus.Stopped) {
- t.cancel();
- }
- var current = await audioRecorder!.current(channel: 0);
- // print(current.status);
- setState(() {
- _current = current!;
- _currentStatus = _current!.status!;
- });
- });
- }
- _resume() async {
- await audioRecorder!.resume();
- Fluttertoast.showToast(msg: "Resume Recording");
- setState(() {
- _recordIcon = Icons.pause;
- colo = Colors.red;
- });
- }
- _pause() async {
- await audioRecorder!.pause();
- Fluttertoast.showToast(msg: "Pause Recording");
- setState(() {
- _recordIcon = Icons.mic;
- colo = Colors.green;
- });
- }
- _stop() async {
- var result = await audioRecorder!.stop();
- Fluttertoast.showToast(msg: "Stop Recording , File Saved");
- widget.saveVoice();
- setState(() {
- _current = result!;
- _currentStatus = _current!.status!;
- _current!.duration = null;
- _recordIcon = Icons.mic;
- stop = false;
- });
- }
- Future<void> _recordo() async {
- Map<Permission, PermissionStatus> statuses = await [
- Permission.microphone,
- Permission.storage,
- ].request();
- print(statuses[Permission.microphone]);
- print(statuses[Permission.storage]);
- if (statuses[Permission.microphone] == PermissionStatus.granted) {
- /* }
- bool hasPermission = await FlutterAudioRecorder.hasPermissions ?? false;
- if (hasPermission) {*/
- await _initial();
- await _start();
- Fluttertoast.showToast(msg: "Start Recording");
- setState(() {
- _currentStatus = RecordingStatus.Recording;
- _recordIcon = Icons.pause;
- colo = Colors.red;
- stop = true;
- });
- } else {
- Fluttertoast.showToast(msg: "Allow App To Use Mic");
- }
- }
- }
|