This commit is contained in:
Hamza Aleghwairyeen
2024-04-09 02:52:52 +03:00
parent 207a87a5f0
commit d68eb5028f
11 changed files with 249 additions and 164 deletions

View File

@@ -1,101 +1,159 @@
import 'dart:io';
// import 'dart:io';
//
// import 'package:flutter/material.dart';
// import 'package:flutter_sound/flutter_sound.dart';
// import 'package:get/get.dart';
// import 'package:permission_handler/permission_handler.dart';
//
// import '../home/map_passenger_controller.dart';
//
// class AudioController extends GetxController {
// final recorder = FlutterSoundRecorder();
// bool isRecording = false;
//
// @override
// void onInit() {
// super.onInit();
// initRecorder();
// }
//
// Future<void> initRecorder() async {
// final status = await Permission.microphone.request();
// if (status != PermissionStatus.granted) {
// if (status.isPermanentlyDenied) {
// // Handle permission permanently denied
// showPermissionDeniedDialog();
// } else if (status.isDenied) {
// // Handle permission denied
// showPermissionDeniedSnackbar();
// } else if (status.isRestricted) {
// // Handle permission restricted
// showPermissionDeniedSnackbar();
// }
// return;
// }
// await recorder.openRecorder();
// recorder.setSubscriptionDuration(const Duration(minutes: 50));
// }
//
// Future<void> startRecording() async {
// if (!recorder.isStopped) {
// await recorder.startRecorder();
// }
// isRecording = true;
// update();
// }
//
// Future<void> stopRecording() async {
// final filePath = await recorder.stopRecorder();
// final audioFile = File(filePath!);
// print('Recorded file path: $audioFile');
// // Now you can send this file to the server
// isRecording = false;
// update();
// }
//
// @override
// void onClose() {
// recorder.stopRecorder();
// super.onClose();
// }
//
// void showPermissionDeniedDialog() {
// showDialog(
// context: Get.context!,
// builder: (context) => AlertDialog(
// title: const Text('Microphone Permission'),
// content:
// const Text('Microphone permission is required to record audio.'),
// actions: [
// TextButton(
// onPressed: () {
// Navigator.of(context).pop();
// openAppSettings();
// },
// child: const Text('Open Settings'),
// ),
// TextButton(
// onPressed: () {
// Navigator.of(context).pop();
// },
// child: const Text('Cancel'),
// ),
// ],
// ),
// );
// }
//
// void showPermissionDeniedSnackbar() {
// Get.snackbar(
// 'Microphone Permission',
// 'Microphone permission is required to record audio.',
// snackPosition: SnackPosition.BOTTOM,
// duration: const Duration(seconds: 5),
// mainButton: TextButton(
// onPressed: () {
// openAppSettings();
// },
// child: const Text(
// 'Open Settings',
// style: TextStyle(color: Colors.white),
// ),
// ),
// );
// }
// }
import 'package:flutter/material.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:get/get.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:permission_handler/permission_handler.dart';
import '../home/map_passenger_controller.dart';
class AudioController extends GetxController {
final recorder = FlutterSoundRecorder();
final flutterSoundHelper = FlutterSoundHelper();
bool isRecording = false;
@override
void onInit() {
super.onInit();
initRecorder();
}
Future<void> startRecording() async {
if (!await flutterSoundHelper.hasPermissions()) {
await flutterSoundHelper.requestPermissions();
}
Future<void> initRecorder() async {
final status = await Permission.microphone.request();
if (status != PermissionStatus.granted) {
if (status.isPermanentlyDenied) {
// Handle permission permanently denied
showPermissionDeniedDialog();
} else {
// Handle permission denied
showPermissionDeniedSnackbar();
}
if (!await flutterSoundHelper.hasPermissions()) {
return;
}
await recorder.openRecorder();
recorder.setSubscriptionDuration(const Duration(minutes: 50));
}
Future<void> startRecording() async {
final controller = Get.find<MapPassengerController>();
final filePath = 'audio_${controller.rideId}.wav'; // Specify the file name
await recorder.startRecorder(toFile: filePath);
await flutterSoundHelper.startRecorder();
isRecording = true;
update();
}
Future<void> stopRecording() async {
final filePath = await recorder.stopRecorder();
final audioFile = File(filePath!);
print('Recorded file path: $audioFile');
// Now you can send this file to the server
if (!isRecording) {
return;
}
await flutterSoundHelper.stopRecorder();
isRecording = false;
update();
}
@override
void onClose() {
recorder.stopRecorder();
super.onClose();
}
void showPermissionDeniedDialog() {
showDialog(
context: Get.context!,
builder: (context) => AlertDialog(
title: Text('Microphone Permission'),
content: Text('Microphone permission is required to record audio.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
openAppSettings();
},
child: Text('Open Settings'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
],
),
);
}
void showPermissionDeniedSnackbar() {
Get.snackbar(
'Microphone Permission',
'Microphone permission is required to record audio.',
snackPosition: SnackPosition.BOTTOM,
duration: Duration(seconds: 5),
mainButton: TextButton(
onPressed: () {
openAppSettings();
},
child: Text(
'Open Settings',
style: TextStyle(color: Colors.white),
),
),
);
}
}
class FlutterSoundHelper {
final flutterSound = FlutterSoundRecorder();
Future<bool> hasPermissions() async {
return await Permission.microphone.isGranted;
}
Future<void> requestPermissions() async {
await Permission.microphone.request();
}
Future<void> startRecorder() async {
await flutterSound.openRecorder();
await flutterSound.startRecorder(toFile: 'audio.wav');
}
Future<void> stopRecorder() async {
await flutterSound.stopRecorder();
await flutterSound.closeRecorder();
}
}