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) { if (processingState == ProcessingState.loading) {
abortExistingConnection(); abortExistingConnection();
} }
if (playResult != null) {
playResult.success(new HashMap<String, Object>());
playResult = null;
}
mediaSources.clear(); mediaSources.clear();
mediaSource = null; mediaSource = null;
if (player != null) { if (player != null) {

View File

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

View File

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

View File

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

View File

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