just_audio
just_audio copied to clipboard
Fix exception on calling LockCachingAudioSource clearCache after the player is stopped
Steps to reproduce:-
final player = AudioPlayer();
final audioSource = LockCachingAudioSource(Uri.parse("stream_link"));
await player.setAudioSource(audioSource);
await player.play()
// After some time
await player.stop()
await audioSource.clearCache()
// or
player.processingStateStream.listen((processingState) {
if processingState == ProcessingState.idle) {
await audioSource.clearCache();
}
});
Exception being mitigated:-
I/ExoPlayerImpl(11100): Release 28530fd [ExoPlayerLib/2.18.7] [joyeuse, Redmi Note 9 Pro, Xiaomi, 29] [goog.exo.core, goog.exo.exoplayer, goog.exo.decoder, goog.exo.datasource, goog.exo.extractor]
E/flutter (11100): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Cannot clear cache while download is in progress
E/flutter (11100): #0 LockCachingAudioSource.clearCache (package:just_audio/just_audio.dart:2769:7)
E/flutter (11100): #1 PlayerBloc.initialize.<anonymous closure> (package:abc/state/ui/player/player_bloc.dart:55:57)
E/flutter (11100): #2 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (11100): #3 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (11100): #4 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (11100): #5 _MultiStreamController.addSync (dart:async/stream_impl.dart:1044:36)
E/flutter (11100): #6 _MultiControllerSink.add (package:rxdart/src/utils/forwarding_stream.dart:130:35)
E/flutter (11100): #7 _StartWithStreamSink.onData (package:rxdart/src/transformers/start_with.dart:12:31)
E/flutter (11100): #8 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (11100): #9 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (11100): #10 _DelayedData.perform (dart:async/stream_impl.dart:515:14)
E/flutter (11100): #11 _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
E/flutter (11100): #12 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
E/flutter (11100): #13 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (11100): #14 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
E/flutter (11100):
Also it looks to be that the http request is not being closed either, I tried extracting the httpClient
and the httpRequest
out of the _fetch
function and calling their close methods respectively at the same place where the downloading flag is being set in this PR, but I'm not sure if it's the best solution. The client needed to be closed with the force flag as well for the request to close. This is based upon what I'm seeing in the network tab of the Flutter Devtools