OpenContacts/lib/clients/audio_cache_client.dart
Nutcake 812148658d
Add iOS support (#18)
* add web support and remove all tabs except chat

* Update README.md

* feat: setup project for xcode builds + add other tabs back

* chore: update macos podfile

* chore: update packages + update build config + add audio playback on iOS/macOS + swap downloader library

* chore: remove ide-specific files

* chore: update readme

* chore: update build.gradle to reflect correct minSdkVersion

* chore: remove generated ic_launcher png files

* chore: update formatting to use 120 char line length

* chore: address use of then method on awaited future

* chore: update app icons for macOS & iOS

* Fix file move not waiting for download finish

---------

Co-authored-by: lumey <lumey@lumey.dev>
Co-authored-by: Garrett <toast@isota.ch>
2023-11-14 20:28:14 +01:00

39 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:ffmpeg_kit_flutter_audio/ffmpeg_kit.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:recon/auxiliary.dart';
import 'package:recon/clients/api_client.dart';
import 'package:recon/models/message.dart';
class AudioCacheClient {
final Future<Directory> _directoryFuture = getTemporaryDirectory();
final bool _isDarwin = Platform.isMacOS || Platform.isIOS;
Future<File> cachedNetworkAudioFile(AudioClipContent clip) async {
final directory = await _directoryFuture;
final fileName = basenameWithoutExtension(clip.assetUri);
final file = File("${directory.path}/$fileName.ogg");
if (!await file.exists()) {
await file.create(recursive: true);
final response = await http.get(Uri.parse(Aux.resdbToHttp(clip.assetUri)));
ApiClient.checkResponseCode(response);
await file.writeAsBytes(response.bodyBytes);
}
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;
}
return file;
}
}