step 7 : convert .aac into .mp3
This commit is contained in:
@@ -7,6 +7,56 @@ abstract class BaseRepository {
|
|||||||
final Dio dio;
|
final Dio dio;
|
||||||
BaseRepository({required this.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
|
// POST PAKE ContentType JSON
|
||||||
Future<Map<String, dynamic>> post({
|
Future<Map<String, dynamic>> post({
|
||||||
required Map<String, dynamic> param,
|
required Map<String, dynamic> param,
|
||||||
@@ -103,4 +153,4 @@ class BaseRepositoryException implements Exception {
|
|||||||
BaseRepositoryException({
|
BaseRepositoryException({
|
||||||
required this.message,
|
required this.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:ffmpeg_kit_flutter_audio/ffmpeg_kit.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
@@ -64,6 +65,7 @@ class RekamScreen extends HookConsumerWidget {
|
|||||||
try {
|
try {
|
||||||
final dir = await getApplicationDocumentsDirectory();
|
final dir = await getApplicationDocumentsDirectory();
|
||||||
final filePath = '${dir.path}/myFile.aac';
|
final filePath = '${dir.path}/myFile.aac';
|
||||||
|
// final filePath = '${dir.path}/myFile.mp3';
|
||||||
audioPath.value = filePath;
|
audioPath.value = filePath;
|
||||||
|
|
||||||
await recorder.value.startRecorder(
|
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 {
|
Future<void> berhentiRekaman() async {
|
||||||
try {
|
try {
|
||||||
await recorder.value.stopRecorder();
|
await recorder.value.stopRecorder();
|
||||||
@@ -84,6 +102,15 @@ class RekamScreen extends HookConsumerWidget {
|
|||||||
isRekam.value = false;
|
isRekam.value = false;
|
||||||
|
|
||||||
// panggil fungsi untuk kirim ke BE
|
// 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) {
|
} catch (e) {
|
||||||
print("Error stop record ${e.toString()}");
|
print("Error stop record ${e.toString()}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,13 @@
|
|||||||
import FlutterMacOS
|
import FlutterMacOS
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
import ffmpeg_kit_flutter_audio
|
||||||
import mobile_scanner
|
import mobile_scanner
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
import shared_preferences_foundation
|
import shared_preferences_foundation
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
|
FFmpegKitFlutterPlugin.register(with: registry.registrar(forPlugin: "FFmpegKitFlutterPlugin"))
|
||||||
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
|
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
|
|||||||
16
pubspec.lock
16
pubspec.lock
@@ -113,6 +113,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.3"
|
version: "2.1.3"
|
||||||
|
ffmpeg_kit_flutter_audio:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: ffmpeg_kit_flutter_audio
|
||||||
|
sha256: "1e6de4d6afdd1b842dde17ef55d9cfa8911d5c4a5858e80f4371487c29e42f8a"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.0.3"
|
||||||
|
ffmpeg_kit_flutter_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: ffmpeg_kit_flutter_platform_interface
|
||||||
|
sha256: addf046ae44e190ad0101b2fde2ad909a3cd08a2a109f6106d2f7048b7abedee
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.2.1"
|
||||||
file:
|
file:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ dependencies:
|
|||||||
toastification: ^2.3.0
|
toastification: ^2.3.0
|
||||||
mobile_scanner: ^6.0.6
|
mobile_scanner: ^6.0.6
|
||||||
flutter_sound: ^9.23.1
|
flutter_sound: ^9.23.1
|
||||||
|
ffmpeg_kit_flutter_audio: ^6.0.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user