import 'dart:io'; // import 'package:flutter_sound/flutter_sound.dart'; import 'package:path_provider/path_provider.dart'; import 'package:get/get.dart'; import 'package:just_audio/just_audio.dart'; import 'package:record/record.dart'; class AudioRecorderController extends GetxController { AudioPlayer audioPlayer = AudioPlayer(); AudioRecorder recorder = AudioRecorder(); bool isRecording = false; bool isPlaying = false; bool isPaused = false; String filePath = ''; String? selectedFilePath; double currentPosition = 0; double totalDuration = 0; Future playSoundFromAssets(String sound) async { try { await audioPlayer.setAsset(sound); audioPlayer.play(); } catch (e) { print("Error playing sound: $e"); } } // Start recording Future startRecording() async { final bool isPermissionGranted = await recorder.hasPermission(); if (!isPermissionGranted) { // RecordingPermissionException('l'); return; } final directory = await getApplicationDocumentsDirectory(); // Generate a unique file name using the current timestamp String fileName = // '${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day}_${Get.find().rideId}.m4a'; '${DateTime.now().year}-${DateTime.now().month}-${DateTime.now().day}.m4a'; filePath = '${directory.path}/$fileName'; // Define the configuration for the recording const config = RecordConfig( // Specify the format, encoder, sample rate, etc., as needed encoder: AudioEncoder.aacLc, // For example, using AAC codec sampleRate: 44100, // Sample rate bitRate: 128000, // Bit rate ); // Start recording to file with the specified configuration await recorder.start(config, path: filePath); isRecording = true; update(); } // Pause recording Future pauseRecording() async { if (isRecording && !isPaused) { await recorder.pause(); isPaused = true; update(); } } // Resume recording Future resumeRecording() async { if (isRecording && isPaused) { await recorder.resume(); isPaused = false; update(); } } // Stop recording Future stopRecording() async { recorder.stop(); isRecording = false; isPaused = false; update(); } // Play the selected recorded file Future 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 pausePlayback() async { if (isPlaying && !isPaused) { await audioPlayer.pause(); isPaused = true; update(); } } // Resume playback Future resumePlayback() async { if (isPlaying && isPaused) { await audioPlayer.play(); isPaused = false; update(); } } // Stop playback Future stopPlayback() async { await audioPlayer.stop(); isPlaying = false; isPaused = false; currentPosition = 0; update(); } // Get a list of recorded files Future> getRecordedFiles() async { final directory = await getApplicationDocumentsDirectory(); final files = await directory.list().toList(); return files .map((file) => file.path) .where((path) => path.endsWith('.m4a')) .toList(); } // Delete a specific recorded file Future deleteRecordedFile(String filePath) async { final file = File(filePath); if (await file.exists()) { await file.delete(); update(); } } // Delete all recorded files Future deleteAllRecordedFiles() async { final directory = await getApplicationDocumentsDirectory(); final files = await directory.list().toList(); for (final file in files) { if (file.path.endsWith('.m4a')) { await deleteRecordedFile(file.path); } } } @override void onClose() { audioPlayer.dispose(); recorder.dispose(); super.onClose(); } }