161 lines
4.2 KiB
Dart
161 lines
4.2 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 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/public/flutter_sound_recorder.dart';
|
|
// import 'package:get/get.dart';
|
|
// // import 'package:flutter_sound/flutter_sound.dart';
|
|
// import 'package:permission_handler/permission_handler.dart';
|
|
//
|
|
// class AudioController extends GetxController {
|
|
// final flutterSoundHelper = FlutterSoundHelper();
|
|
//
|
|
// bool isRecording = false;
|
|
//
|
|
// Future<void> startRecording() async {
|
|
// if (!await flutterSoundHelper.hasPermissions()) {
|
|
// await flutterSoundHelper.requestPermissions();
|
|
// }
|
|
//
|
|
// if (!await flutterSoundHelper.hasPermissions()) {
|
|
// return;
|
|
// }
|
|
//
|
|
// await flutterSoundHelper.startRecorder();
|
|
// isRecording = true;
|
|
// }
|
|
//
|
|
// Future<void> stopRecording() async {
|
|
// if (!isRecording) {
|
|
// return;
|
|
// }
|
|
//
|
|
// await flutterSoundHelper.stopRecorder();
|
|
// isRecording = false;
|
|
// }
|
|
// }
|
|
//
|
|
// 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();
|
|
// }
|
|
// }
|