This commit is contained in:
Hamza-Ayed
2024-09-04 14:49:36 +03:00
parent 04c47d3431
commit e23709c924
19 changed files with 568 additions and 266 deletions

View File

@@ -1,6 +1,5 @@
import 'dart:io';
import 'package:SEFER/controller/home/map_passenger_controller.dart';
// import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart';
@@ -13,12 +12,23 @@ class AudioRecorderController extends GetxController {
AudioRecorder recorder = AudioRecorder();
bool isRecording = false;
bool isPlaying = false;
bool isPaused = false;
String filePath = '';
String? selectedFilePath;
double currentPosition = 0;
double totalDuration = 0;
String? selectedFile;
Future<void> playSoundFromAssets(String sound) async {
try {
await audioPlayer.setAsset(sound);
audioPlayer.play();
} catch (e) {
print("Error playing sound: $e");
}
}
// Start recording
Future<void> startRecording() async {
final bool isPermissionGranted = await recorder.hasPermission();
if (!isPermissionGranted) {
@@ -48,26 +58,76 @@ class AudioRecorderController extends GetxController {
update();
}
Future<void> stopRecording() async {
final path = await recorder.stop();
isRecording = false;
update();
}
Future<void> playRecording() async {
if (filePath != null) {
await audioPlayer.setFilePath(filePath!);
totalDuration = audioPlayer.duration?.inSeconds.toDouble() ?? 0;
audioPlayer.play();
audioPlayer.positionStream.listen((position) {
currentPosition = position.inSeconds.toDouble();
});
selectedFilePath = filePath;
// Pause recording
Future<void> pauseRecording() async {
if (isRecording && !isPaused) {
await recorder.pause();
isPaused = true;
update();
}
}
// Resume recording
Future<void> resumeRecording() async {
if (isRecording && isPaused) {
await recorder.resume();
isPaused = false;
update();
}
}
// Stop recording
Future<void> stopRecording() async {
await recorder.stop();
isRecording = false;
isPaused = false;
update();
}
// Play the selected recorded file
Future<void> playRecordedFile(String filePath) async {
await audioPlayer.setFilePath(filePath);
totalDuration = audioPlayer.duration?.inSeconds.toDouble() ?? 0;
audioPlayer.play();
isPlaying = true;
isPaused = false;
audioPlayer.positionStream.listen((position) {
currentPosition = position.inSeconds.toDouble();
update();
});
selectedFilePath = filePath;
update();
}
// Pause playback
Future<void> pausePlayback() async {
if (isPlaying && !isPaused) {
await audioPlayer.pause();
isPaused = true;
update();
}
}
// Resume playback
Future<void> resumePlayback() async {
if (isPlaying && isPaused) {
await audioPlayer.play();
isPaused = false;
update();
}
}
// Stop playback
Future<void> stopPlayback() async {
await audioPlayer.stop();
isPlaying = false;
isPaused = false;
currentPosition = 0;
update();
}
// Get a list of recorded files
Future<List<String>> getRecordedFiles() async {
final directory = await getApplicationDocumentsDirectory();
final files = await directory.list().toList();
@@ -77,25 +137,16 @@ class AudioRecorderController extends GetxController {
.toList();
}
Future<void> playRecordedFile(String filePath) async {
await audioPlayer.setFilePath(filePath);
totalDuration = audioPlayer.duration?.inSeconds.toDouble() ?? 0;
audioPlayer.play();
audioPlayer.positionStream.listen((position) {
currentPosition = position.inSeconds.toDouble();
});
update();
}
// Delete a specific recorded file
Future<void> deleteRecordedFile(String filePath) async {
final file = File(filePath);
if (await file.exists()) {
await file.delete();
await getRecordedFiles();
} else {}
update();
}
}
// Delete all recorded files
Future<void> deleteAllRecordedFiles() async {
final directory = await getApplicationDocumentsDirectory();
final files = await directory.list().toList();