Skip to content

Commit

Permalink
Merge branch 'silgam-fix/memory-leak' into minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Oct 30, 2024
2 parents aa8d067 + f904737 commit 7ad9127
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions just_audio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.9.42

* Fix dealloc crash on iOS/macOS (@cristian1980).
* Fix Dart memory leak on dispose (@MinSeungHyun).

## 0.9.41

Expand Down
16 changes: 13 additions & 3 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class AudioPlayer {
/// subscribe to the new platform's events.
StreamSubscription<PlayerDataMessage>? _playerDataSubscription;

StreamSubscription<AndroidAudioAttributes>?
_androidAudioAttributesSubscription;
StreamSubscription<void>? _becomingNoisyEventSubscription;
StreamSubscription<AudioInterruptionEvent>? _interruptionEventSubscription;

final String _id;
final _proxy = _ProxyHttpServer();
AudioSource? _audioSource;
Expand Down Expand Up @@ -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<AndroidAudioAttributes>()
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ad9127

Please sign in to comment.