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 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(); } return; } await recorder.openRecorder(); recorder.setSubscriptionDuration(const Duration(minutes: 50)); } Future startRecording() async { final controller = Get.find(); final filePath = 'audio_${controller.rideId}.wav'; // Specify the file name await recorder.startRecorder(toFile: filePath); isRecording = true; update(); } Future 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: 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), ), ), ); } }