Files
tripz/lib/controller/functions/audio_recorder_controller.dart
Hamza Aleghwairyeen 6a4a549211 4/7/5
2024-04-07 23:59:49 +03:00

102 lines
2.6 KiB
Dart

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 {
// Handle permission denied
showPermissionDeniedSnackbar();
}
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);
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: 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),
),
),
);
}
}