You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/widgets/sound/record_sound.dart

231 lines
7.6 KiB
Dart

import 'dart:io';
3 years ago
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
3 years ago
import 'package:permission_handler/permission_handler.dart';
import 'package:rive/rive.dart';
import 'package:test_sa/views/widgets/buttons/app_icon_button2.dart';
import 'package:test_sa/views/widgets/buttons/app_small_button.dart';
import 'package:test_sa/views/widgets/sound/sound_player.dart';
import 'package:record_mp3/record_mp3.dart';
3 years ago
import '../../app_style/sizing.dart';
3 years ago
class RecordSound extends StatefulWidget {
final Function(String) onRecord;
2 years ago
final bool enabled;
2 years ago
const RecordSound({Key key, @required this.onRecord, this.enabled = true}) : super(key: key);
3 years ago
@override
State<RecordSound> createState() => _RecordSoundState();
}
class _RecordSoundState extends State<RecordSound> {
// FlutterSoundRecorder _myRecorder = FlutterSoundRecorder();
3 years ago
bool _recorderIsOpened = false;
bool _recording = false;
String _record;
Artboard _rive;
@override
void setState(VoidCallback fn) {
if (mounted) super.setState(fn);
3 years ago
}
3 years ago
@override
void initState() {
super.initState();
_recorderIsOpened = true;
// RecordMp3.instance.start(recordFilePath, (type) {
// // record fail callback
// });
// _myRecorder.openRecorder().then((value) {
// _recorderIsOpened = true;
// setState(() {});
// });
3 years ago
// Load the animation file from the bundle, note that you could also
// download this. The RiveFile just expects a list of bytes.
rootBundle.load('assets/rives/recording.riv').then(
(data) async {
3 years ago
// Load the RiveFile from the binary data.
final file = RiveFile.import(data);
// The artboard is the root of the animation and gets drawn in the
// Rive widget.
final artboard = file.mainArtboard;
// Add a controller to play back a known animation on the main/default
// artboard.We store a reference to it so we can toggle playback.
artboard.addController(SimpleAnimation('recording'));
_rive = artboard;
setState(() {});
},
);
}
@override
void dispose() {
// Be careful : you must `close` the audio session when you have finished with it.
RecordMp3.instance.stop();
//_myRecorder.closeRecorder();
// _myRecorder = null;
3 years ago
super.dispose();
}
String recordingFileDirectory;
3 years ago
_startRecording() async {
// await Permission.camera
PermissionStatus status = await Permission.microphone.request();
if (!status.isGranted) {
PermissionStatus status = await Permission.microphone.request();
2 years ago
if (!status.isGranted) {
Fluttertoast.showToast(msg: "Permission Denied");
return;
}
3 years ago
}
_rive.addController(SimpleAnimation('recording'));
if (!_recorderIsOpened) {
// await _myRecorder.openRecorder();
3 years ago
_recorderIsOpened = true;
}
final Directory tempDir = await getTemporaryDirectory();
recordingFileDirectory = "${tempDir.path}/record_${DateTime.now().millisecondsSinceEpoch}.mp3";
RecordMp3.instance.start(recordingFileDirectory, (type) {
// record fail callback
});
3 years ago
// await _myRecorder.startRecorder(toFile: "record_${DateTime.now().millisecondsSinceEpoch}.mp3", codec: Codec.aacADTS, sampleRate: 360000, bitRate: 360000);
3 years ago
_recording = true;
setState(() {});
}
_stopRecording() async {
if (!_recording) {
3 years ago
setState(() {});
return;
}
RecordMp3.instance.stop();
//String path = (await _myRecorder.stopRecorder()).toString();
_record = recordingFileDirectory;
widget.onRecord(recordingFileDirectory);
3 years ago
_recording = false;
setState(() {});
3 years ago
}
_cancelRecording() async {
if (!_recording) return;
RecordMp3.instance.stop();
// String path = await _myRecorder.stopRecorder();
// _myRecorder.deleteRecord(fileName: path);
3 years ago
_rive.addController(SimpleAnimation('delete'));
// rebuild();
_recording = false;
await Future.delayed(const Duration(seconds: 1));
if (!_recording) setState(() {});
3 years ago
// _message.memoryAudio.;
}
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.only(left: 12, right: 0),
decoration: BoxDecoration(
color: const Color(0xfff5f5f5),
border: Border.all(
color: const Color(0xffefefef),
),
borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
),
child: Column(
children: [
Row(
children: [
Expanded(
child: _recording
? Row(
children: [
ASmallButton(
text: "done",
onPressed: () {
_stopRecording();
},
3 years ago
),
Expanded(
child: Stack(
children: [
SizedBox(
height: 24 * AppStyle.getScaleFactor(context),
child: Rive(
artboard: _rive,
)),
InkWell(
child: SizedBox(
height: 32 * AppStyle.getScaleFactor(context),
width: MediaQuery.of(context).size.width,
),
onTap: () {
_cancelRecording();
},
),
],
),
),
],
3 years ago
)
: _record != null
? Row(
children: [
Expanded(child: ASoundPlayer(audio: _record)),
AIconButton2(
iconData: Icons.delete,
onPressed: () {
widget.onRecord(null);
_record = null;
setState(() {});
},
)
],
)
: const Text("Record Voice"),
),
Material(
color: Colors.transparent,
child: GestureDetector(
//key: ValueKey("voice"),
2 years ago
onTapDown: widget.enabled
? (TapDownDetails details) async {
_startRecording();
}
: null,
onTapUp: widget.enabled
? (TapUpDetails details) async {
_stopRecording();
}
: null,
onTapCancel: widget.enabled
? () async {
_cancelRecording();
}
: null,
2 years ago
//key: ValueKey("voice"),
child: const Padding(padding: EdgeInsets.all(12.0), child: Icon(Icons.mic)),
),
3 years ago
),
],
),
],
),
3 years ago
);
}
}