step 7 : convert .aac into .mp3

This commit is contained in:
sindhu
2025-02-22 06:59:38 +07:00
parent 5f1917c667
commit dc973178c6
5 changed files with 97 additions and 1 deletions

View File

@@ -7,6 +7,56 @@ abstract class BaseRepository {
final Dio dio;
BaseRepository({required this.dio});
// POST audio
Future<Map<String, dynamic>> postAudio({
required String filePath,
required String service,
String? token,
}) async {
try {
FormData formData = FormData.fromMap({
"audio":
await MultipartFile.fromFile(filePath, filename: "rekaman.mp3"),
});
final response = await dio.post(
// Constant.baseUrl + service,
service,
data: formData,
options: Options(
headers: token != null
? {
HttpHeaders.contentTypeHeader: "multipart/form-data",
HttpHeaders.authorizationHeader: "Bearer $token",
}
: {
HttpHeaders.authorizationHeader: "Bearer $token"
},
contentType: "application/json",
),
);
if (response.statusCode != 200) {
throw BaseRepositoryException(
message: "Invalid Http Response ${response.statusCode}",
);
}
Map<String, dynamic> jsonData = jsonDecode(response.data);
if (jsonData["status"] != "OK") {
throw BaseRepositoryException(
message: jsonData["message"],
);
} else {
return jsonData;
}
} on DioException catch (e) {
throw BaseRepositoryException(message: e.message ?? "");
} on SocketException catch (e) {
throw BaseRepositoryException(message: e.message);
} on BaseRepositoryException catch (e) {
throw BaseRepositoryException(message: e.message);
}
}
// POST PAKE ContentType JSON
Future<Map<String, dynamic>> post({
required Map<String, dynamic> param,
@@ -103,4 +153,4 @@ class BaseRepositoryException implements Exception {
BaseRepositoryException({
required this.message,
});
}
}

View File

@@ -1,3 +1,4 @@
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';
@@ -64,6 +65,7 @@ class RekamScreen extends HookConsumerWidget {
try {
final dir = await getApplicationDocumentsDirectory();
final filePath = '${dir.path}/myFile.aac';
// final filePath = '${dir.path}/myFile.mp3';
audioPath.value = filePath;
await recorder.value.startRecorder(
@@ -77,6 +79,22 @@ class RekamScreen extends HookConsumerWidget {
}
}
Future<String?> convertToMp3(String inputPath) async {
final dir = await getApplicationDocumentsDirectory();
final outputPath = "${dir.path}/output.mp3";
// Jalankan perintah FFmpeg untuk konversi
await FFmpegKit.execute(
'-i $inputPath -codec:a libmp3lame -qscale:a 2 $outputPath');
// Pastikan file MP3 berhasil dibuat
if (File(outputPath).existsSync()) {
return outputPath;
} else {
return null;
}
}
Future<void> berhentiRekaman() async {
try {
await recorder.value.stopRecorder();
@@ -84,6 +102,15 @@ class RekamScreen extends HookConsumerWidget {
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()}");
}