For now in my controller i have wriiten logic that for android platform it cache audio and the play which works correctly and for ios it should play directly from url but i want to cache in ios also to reduce time + I also observed that the loading time is also very high caching the file and then playing it how can i reduce that too
Error
flutter: ^[[31mAudioPlayers Exception: AudioPlayerException(
DeviceFileSource(path: /var/mobile/Containers/Data/Application/879CF527-F744-4A78-9781-C5C199C9D26B/Library/Caches/libCachedImageData/95a93610-726b-1024-be1f-bb3cc5bb43ef.x-hx-aac-adts, mimeType: null),
PlatformException(DarwinAudioError, Failed to set source. For troubleshooting, see https://github.com/bluefireteam/audioplayers/blob/main/troubleshooting.md, AVPlayerItem.Status.failed on setSourceUrl: Unknown error, null)<…>
audio_player_controller.dart
void playAudio(String url, bool newSession) async {
state = true;
if (Platform.isAndroid) {
//Takes a lot of loading time due to downloading the audio file caching it and sending back the url
final res = await _audioPlayerRepository.getAudio(url);
res.fold(
(error) => debugPrint(
'Unable to get audio file from the cache storage. $error'),
(audioFilePath) async {
final res = await _audioPlayerRepository.playAudio(audioFilePath);
res.fold(
(error) => debugPrint(error.message),
(success) {
debugPrint('successfully played $url');
newSession
? _ref
.read(audioSessiontimerProvider.notifier)
.startSessionTimer()
: ();
},
);
});
} else {
//Platform is ios look for caching problem till then play audio
final res = await _audioPlayerRepository.playAudioFromUrl(url);
res.fold(
(error) => debugPrint(error.message),
(success) {
debugPrint('successfully played $url');
newSession
? _ref
.read(audioSessiontimerProvider.notifier)
.startSessionTimer()
: ();
},
);
}
state = false;
}
audio_player_repository.dart
FutureEither<String> getAudio(String url) async {
try {
FileInfo? fileInfo = await _defaultCacheManager.getFileFromCache(url);
fileInfo ??= await DefaultCacheManager().downloadFile(url);
return right(fileInfo.file.path);
} catch (error) {
return left(
Failure(
error.toString(),
),
);
}
}
default_cache_manager.dart
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
/// The DefaultCacheManager that can be easily used directly. The code of
/// this implementation can be used as inspiration for more complex cache
/// managers.
class DefaultCacheManager extends CacheManager with ImageCacheManager {
static const key = 'libCachedImageData';
static final DefaultCacheManager _instance = DefaultCacheManager._();
factory DefaultCacheManager() {
return _instance;
}
DefaultCacheManager._() : super(Config(key));
}