diff --git a/just_audio/CHANGELOG.md b/just_audio/CHANGELOG.md index 11275f04f..03c0596e7 100644 --- a/just_audio/CHANGELOG.md +++ b/just_audio/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.9.42 * Fix dealloc crash on iOS/macOS (@cristian1980). +* Fix Dart memory leak on dispose (@MinSeungHyun). ## 0.9.41 diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index a445db1de..057028a6e 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -97,6 +97,11 @@ class AudioPlayer { /// subscribe to the new platform's events. StreamSubscription? _playerDataSubscription; + StreamSubscription? + _androidAudioAttributesSubscription; + StreamSubscription? _becomingNoisyEventSubscription; + StreamSubscription? _interruptionEventSubscription; + final String _id; final _proxy = _ProxyHttpServer(); AudioSource? _audioSource; @@ -285,7 +290,7 @@ class AudioPlayer { // Respond to changes to AndroidAudioAttributes configuration. if (androidApplyAudioAttributes && _isAndroid()) { AudioSession.instance.then((audioSession) { - audioSession.configurationStream + _androidAudioAttributesSubscription = audioSession.configurationStream .map((conf) => conf.androidAudioAttributes) .where((attributes) => attributes != null) .cast() @@ -295,10 +300,12 @@ class AudioPlayer { } if (handleInterruptions) { AudioSession.instance.then((session) { - session.becomingNoisyEventStream.listen((_) { + _becomingNoisyEventSubscription = + session.becomingNoisyEventStream.listen((_) { pause(); }); - session.interruptionEventStream.listen((event) { + _interruptionEventSubscription = + session.interruptionEventStream.listen((event) { if (event.begin) { switch (event.type) { case AudioInterruptionType.duck: @@ -1255,6 +1262,9 @@ class AudioPlayer { await _pitchSubject.close(); await _sequenceSubject.close(); await _shuffleIndicesSubject.close(); + await _androidAudioAttributesSubscription?.cancel(); + await _becomingNoisyEventSubscription?.cancel(); + await _interruptionEventSubscription?.cancel(); } /// Switch to using the native platform when [active] is `true` and using the