2023-05-20 15:09:22 -04:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-11-14 15:28:14 -04:00
|
|
|
import 'package:ffmpeg_kit_flutter_audio/ffmpeg_kit.dart';
|
2023-05-20 15:09:22 -04:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
2024-07-15 00:23:04 -04:00
|
|
|
import 'package:OpenContacts/auxiliary.dart';
|
|
|
|
import 'package:OpenContacts/clients/api_client.dart';
|
|
|
|
import 'package:OpenContacts/models/message.dart';
|
2023-05-20 15:09:22 -04:00
|
|
|
|
|
|
|
class AudioCacheClient {
|
|
|
|
final Future<Directory> _directoryFuture = getTemporaryDirectory();
|
2023-11-14 15:28:14 -04:00
|
|
|
final bool _isDarwin = Platform.isMacOS || Platform.isIOS;
|
2023-05-20 15:09:22 -04:00
|
|
|
|
|
|
|
Future<File> cachedNetworkAudioFile(AudioClipContent clip) async {
|
|
|
|
final directory = await _directoryFuture;
|
2023-11-14 15:28:14 -04:00
|
|
|
final fileName = basenameWithoutExtension(clip.assetUri);
|
|
|
|
final file = File("${directory.path}/$fileName.ogg");
|
2023-05-20 15:09:22 -04:00
|
|
|
if (!await file.exists()) {
|
|
|
|
await file.create(recursive: true);
|
2023-09-29 03:51:46 -04:00
|
|
|
final response = await http.get(Uri.parse(Aux.resdbToHttp(clip.assetUri)));
|
2023-05-25 09:50:38 -04:00
|
|
|
ApiClient.checkResponseCode(response);
|
2023-05-20 15:09:22 -04:00
|
|
|
await file.writeAsBytes(response.bodyBytes);
|
|
|
|
}
|
2023-11-14 15:28:14 -04:00
|
|
|
if (_isDarwin) {
|
|
|
|
final wavFile = File("${directory.path}/$fileName.wav");
|
|
|
|
final wavFileExists = await wavFile.exists();
|
|
|
|
if (wavFileExists && await wavFile.length() == 0) {
|
|
|
|
await wavFile.delete();
|
|
|
|
}
|
|
|
|
if (!wavFileExists) {
|
|
|
|
await wavFile.create(recursive: true);
|
|
|
|
await FFmpegKit.executeAsync("-y -acodec libvorbis -i ${file.path} -acodec pcm_s16le ${wavFile.path}");
|
|
|
|
}
|
|
|
|
return wavFile;
|
|
|
|
}
|
2023-05-20 15:09:22 -04:00
|
|
|
return file;
|
|
|
|
}
|
2023-11-14 15:28:14 -04:00
|
|
|
}
|