Handle simultanous play/load requests.

This commit is contained in:
Ryan Heise 2020-12-22 16:27:16 +11:00
parent aa41a7506e
commit 1dbc3cf801
5 changed files with 24 additions and 12 deletions

View File

@ -695,6 +695,10 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
if (processingState == ProcessingState.loading) {
abortExistingConnection();
}
if (playResult != null) {
playResult.success(new HashMap<String, Object>());
playResult = null;
}
mediaSources.clear();
mediaSource = null;
if (player != null) {

View File

@ -134,7 +134,7 @@ packages:
name: just_audio_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
version: "0.2.1"
matcher:
dependency: transitive
description:

View File

@ -687,12 +687,16 @@ class AudioPlayer {
_playCompleter = Completer();
final audioSession = await AudioSession.instance;
if (await audioSession.setActive(true)) {
final shouldActivate = _audioSource != null;
if (shouldActivate) {
// TODO: rewrite this to more cleanly handle simultaneous load/play
// requests which each may result in platform play requests.
final requireActive = _audioSource != null;
if (requireActive) {
if (_active) {
// If the native platform is already active, send it a play request.
await (await _platform).play(PlayRequest());
_playCompleter.complete();
// NOTE: If a load() request happens simultaneously, this may result
// in two play requests being sent. The platform implementation should
// ignore the second play request since it is already playing.
_sendPlayRequest(await _platform);
} else {
// If the native platform wasn't already active, activating it will
// implicitly restore the playing state and send a play request.
@ -702,9 +706,8 @@ class AudioPlayer {
} else {
// Revert if we fail to activate the audio session.
_playingSubject.add(false);
_playCompleter.complete();
}
await _playCompleter.future;
await _playCompleter?.future;
_playCompleter = null;
}
@ -727,6 +730,13 @@ class AudioPlayer {
await (await _platform).pause(PauseRequest());
}
Future<void> _sendPlayRequest(AudioPlayerPlatform platform) async {
await platform.play(PlayRequest());
if (_playCompleter?.isCompleted == false) {
_playCompleter.complete();
}
}
/// Stops playing audio and releases decoders and other native platform
/// resources needed to play audio. The current audio source state will be
/// retained and playback can be resumed at a later point in time.
@ -981,7 +991,7 @@ class AudioPlayer {
? ShuffleModeMessage.all
: ShuffleModeMessage.none));
if (playing) {
platform.play(PlayRequest()).then((_) => _playCompleter?.complete());
_sendPlayRequest(platform);
}
}
if (audioSource != null) {
@ -2081,8 +2091,6 @@ class _IdleAudioPlayer extends AudioPlayerPlatform {
@override
Future<DisposeResponse> dispose(DisposeRequest request) async {
await _sequenceSubscription.cancel();
await _eventSubject.close();
return DisposeResponse();
}

View File

@ -204,7 +204,7 @@ packages:
name: just_audio_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
version: "0.2.1"
logging:
dependency: transitive
description:

View File

@ -9,7 +9,7 @@ environment:
dependencies:
just_audio_platform_interface: ^2.0.0
just_audio_web: ^0.2.0
just_audio_web: ^0.2.1
audio_session: ^0.0.9
rxdart: ">= 0.24.1 < 0.26.0"
path: ^1.6.4