step 9 : proses rekaman dan upload rekaman ke BE

This commit is contained in:
sindhu
2025-02-22 08:53:26 +07:00
parent c6a6dba074
commit a717e32448
7 changed files with 318 additions and 33 deletions

View File

@@ -2,15 +2,21 @@ import 'package:ffmpeg_kit_flutter_audio/ffmpeg_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fluttervoice2text/app/route.dart';
import 'package:fluttervoice2text/model/edit_voice_to_text_model.dart';
import 'package:path_provider/path_provider.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:toastification/toastification.dart';
import 'dart:io';
import '../../app/constant.dart';
import '../../provider/current_user_provider.dart';
import '../../provider/voice_to_text_provider.dart';
import '../home/list_riwayat_rekaman_provider.dart';
import 'upload_rekam_provider.dart';
class RekamScreen extends HookConsumerWidget {
const RekamScreen({super.key});
@@ -24,11 +30,16 @@ class RekamScreen extends HookConsumerWidget {
final isRekam = useState<bool>(false);
final judulTombol = useState<String>("MULAI REKAM");
final qrCodeStr = useState<String>("");
final qrCodeStr = useState<String>("Scan QRCode untuk dapat merekam");
final isSelesaiRekam = useState<bool>(false);
final audioPath = useState<String>("");
final recorder = useState(FlutterSoundRecorder());
final player = useState(FlutterSoundPlayer());
final isLoadingUpload = useState<bool>(false);
final currentUser = ref.watch(currentUserProvider);
final host = currentUser?.host ?? "";
final userId = currentUser?.model.userId ?? "";
final awalan = useState("Info :");
Future<void> requestPermissions() async {
await Permission.microphone.request();
@@ -57,6 +68,7 @@ class RekamScreen extends HookConsumerWidget {
if (scannedBarcode.isNotEmpty) {
ref.read(barcodeX.notifier).state = barcodes.barcodes.first;
qrCodeStr.value = scannedBarcode;
awalan.value = "QrCode : ";
}
}
}
@@ -95,27 +107,6 @@ class RekamScreen extends HookConsumerWidget {
}
}
Future<void> berhentiRekaman() async {
try {
await recorder.value.stopRecorder();
isSelesaiRekam.value = false;
isRekam.value = false;
// panggil fungsi untuk kirim ke BE
if (audioPath.value.isNotEmpty) {
String? mp3Path = await convertToMp3(audioPath.value);
if (mp3Path != null) {
// await uploadAudioFile(mp3Path);
print('mp3 convert $mp3Path');
} else {
print("Konversi gagal!");
}
}
} catch (e) {
print("Error stop record ${e.toString()}");
}
}
Future<void> playRecording() async {
try {
if (audioPath.value.isNotEmpty && File(audioPath.value).existsSync()) {
@@ -128,6 +119,148 @@ class RekamScreen extends HookConsumerWidget {
}
}
Future<void> berhentiRekaman() async {
try {
await recorder.value.stopRecorder();
isSelesaiRekam.value = false;
isRekam.value = false;
// panggil fungsi untuk kirim ke BE
if (audioPath.value.isNotEmpty) {
String? mp3Path = await convertToMp3(audioPath.value);
if (mp3Path != null) {
// await uploadAudioFile(mp3Path);
ref.read(uploadRekamProvider.notifier).uploadRekam(
host: host,
filePath: mp3Path,
userId: userId,
qrCodeStr: qrCodeStr.value,
);
print('mp3 convert $mp3Path');
} else {
print("Konversi gagal!");
}
}
} catch (e) {
print("Error stop record ${e.toString()}");
}
}
void showLongToast(
String title,
String message,
String typeToast,
Duration waktu,
) {
if (typeToast == "success") {
toastification.show(
context: context,
title: Text(title),
description: Text(message),
autoCloseDuration: waktu,
type: ToastificationType.success,
style: ToastificationStyle.fillColored,
);
} else if (typeToast == "error") {
toastification.show(
context: context,
title: Text(title),
description: Text(message),
autoCloseDuration: waktu,
type: ToastificationType.error,
style: ToastificationStyle.fillColored,
);
} else if (typeToast == "warning") {
toastification.show(
context: context,
title: Text(title),
description: Text(message),
autoCloseDuration: waktu,
type: ToastificationType.warning,
style: ToastificationStyle.fillColored,
);
}
}
// list Riwayat Rekam
ref.listen(listRiwayatRekamanProvider, (prev, next) {
if (next is ListRiwayatRekamanStateLoading) {
isLoadingUpload.value = true;
} else if (next is ListRiwayatRekamanStateError) {
isLoadingUpload.value = false;
// errorMessage.value = next.message;
showLongToast(
'Error',
next.message,
'error',
Duration(seconds: 3),
);
} else if (next is ListRiwayatRekamanStateDone) {
isLoadingUpload.value = false;
Navigator.of(context).pop();
Navigator.of(context).pushNamed(
editRekamRoute,
);
}
});
// listen upload rekam
ref.listen(uploadRekamProvider, (prev, next) {
if (next is UploadRekamStateLoading) {
isLoadingUpload.value = true;
} else if (next is UploadRekamStateError) {
isLoadingUpload.value = false;
// errorMessage.value = next.message;
print("Err : ${next.message}");
showLongToast(
'Error',
next.message,
'error',
Duration(seconds: 3),
);
} else if (next is UploadRekamStateDone) {
ref.read(barcodeX.notifier).state = Barcode();
isRekam.value = false;
isSelesaiRekam.value = false;
isLoadingUpload.value = false;
ref.read(selectedVoiceIdx.notifier).state = next.model[0].Voice2text_ID;
ref.read(selectedEdit.notifier).state = EditVoiceToTextModel(
Voice2text_Created: next.model[0].Voice2text_Created,
Voice2text_ID: next.model[0].Voice2text_ID,
Voice2text_IsActive: next.model[0].Voice2text_IsActive,
Voice2text_JsonData: next.model[0].Voice2text_JsonData,
Voice2text_Note: next.model[0].Voice2text_Note,
Voice2text_Text: next.model[0].Voice2text_Text,
Voice2text_Updated: next.model[0].Voice2text_Updated,
Voice2text_Url: next.model[0].Voice2text_Url,
Voice2text_User_ID: next.model[0].Voice2text_User_ID,
);
ref.read(listRiwayatRekamanProvider.notifier).listRiwayatRekaman(
host: host,
userId: userId,
);
}
});
// loading proses upload useEffect
useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((timestamp) {
if (isLoadingUpload.value == true) {
showLongToast(
'Warning',
'Sedang Upload Rekaman',
'warning',
Duration(seconds: 3),
);
}
});
return () {};
}, [isLoadingUpload.value]);
return Scaffold(
backgroundColor: Constant.bgGrey,
bottomNavigationBar: BottomAppBar(
@@ -225,11 +358,9 @@ class RekamScreen extends HookConsumerWidget {
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: 10),
color: Constant.inputanGrey,
child: Text("Info: ${qrCodeStr.value}",
child: Text("${awalan.value} ${qrCodeStr.value}",
style: TextStyle(color: Colors.white)),
),
SizedBox(height: 40),
ElevatedButton(onPressed: playRecording, child: Text('Play')),
],
),
),