Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for issues #518 #870 #1234

Open
wants to merge 2 commits into
base: minor
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions just_audio_background/lib/just_audio_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@ class _JustAudioPlayer extends AudioPlayerPlatform {
late final _PlayerAudioHandler _playerAudioHandler;

_JustAudioPlayer({required this.initRequest}) : super(initRequest.id) {
_playerAudioHandler = _PlayerAudioHandler(initRequest);
_playerAudioHandler = _PlayerAudioHandler(
initRequest,
addPlaybackEventMessageError: (error, stackTrace) {
if (eventController.isClosed) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workaround will not work.
eventController already closed, because disposePlayer called and after that player?.release();

Future<void> release() async {
    eventController.close();
    await _audioHandler.stop();
  }

Error.throwWithStackTrace(error, stackTrace);
}
eventController.addError(error, stackTrace);
},
);
_audioHandler.inner = _playerAudioHandler;
_audioHandler.playbackState.listen((playbackState) {
broadcastPlaybackEvent();
Expand Down Expand Up @@ -399,18 +407,29 @@ class _PlayerAudioHandler extends BaseAudioHandler

List<MediaItem>? get currentQueue => queue.nvalue;

_PlayerAudioHandler(InitRequest initRequest) {
void Function(Object error, StackTrace stackTrace)
addPlaybackEventMessageError;

_PlayerAudioHandler(
InitRequest initRequest, {
required this.addPlaybackEventMessageError,
}) {
_init(initRequest);
}

Future<void> _init(InitRequest initRequest) async {
final player = await _platform.init(initRequest);
_playerCompleter.complete(player);

final playbackEventMessageStream = player.playbackEventMessageStream;
playbackEventMessageStream.listen((event) {
_justAudioEvent = event;
_broadcastState();
});

playbackEventMessageStream.listen(
(event) {
_justAudioEvent = event;
_broadcastState();
},
onError: addPlaybackEventMessageError,
);
playbackEventMessageStream
.map((event) => event.icyMetadata)
.distinct()
Expand All @@ -419,7 +438,7 @@ class _PlayerAudioHandler extends BaseAudioHandler
'type': 'icyMetadata',
'value': icyMetadata,
});
});
}, onError: (_) {});
playbackEventMessageStream
.map((event) => event.androidAudioSessionId)
.distinct()
Expand All @@ -428,7 +447,7 @@ class _PlayerAudioHandler extends BaseAudioHandler
'type': 'androidAudioSessionId',
'value': audioSessionId,
});
});
}, onError: (_) {});
playbackEventMessageStream
.map((event) => TrackInfo(event.currentIndex, event.duration))
.distinct()
Expand All @@ -455,7 +474,7 @@ class _PlayerAudioHandler extends BaseAudioHandler
}
mediaItem.add(currentMediaItem!);
}
});
}, onError: (_) {});
}

@override
Expand Down