play implicitly loads audio source if/when available.

This commit is contained in:
Ryan Heise 2020-12-22 01:36:40 +11:00
parent 6e283966de
commit 98034cb584
1 changed files with 16 additions and 9 deletions

View File

@ -514,7 +514,9 @@ class AudioPlayer {
/// its duration as soon as it is known, or `null` if that information is /// its duration as soon as it is known, or `null` if that information is
/// unavailable. Set [preload] to `false` if you would prefer to delay loading /// unavailable. Set [preload] to `false` if you would prefer to delay loading
/// until some later point, via an explicit call to [load]. If [preload] is /// until some later point, via an explicit call to [load]. If [preload] is
/// `false`, a `null` duration will be returned. /// `false`, a `null` duration will be returned. Note that the [preload]
/// option will automatically be assumed as `true` if `playing` is currently
/// `true`.
/// ///
/// Optionally specify [initialPosition] and [initialIndex] to seek to an /// Optionally specify [initialPosition] and [initialIndex] to seek to an
/// initial position within a particular item (defaulting to position zero of /// initial position within a particular item (defaulting to position zero of
@ -534,9 +536,6 @@ class AudioPlayer {
Duration initialPosition, Duration initialPosition,
}) async { }) async {
if (_disposed) return null; if (_disposed) return null;
// Idea: always keep the idle player around and make it possible
// to switch between idle and active players without disposing either
// one.
_audioSource = null; _audioSource = null;
_initialSeekValues = _initialSeekValues =
_InitialSeekValues(position: initialPosition, index: initialIndex); _InitialSeekValues(position: initialPosition, index: initialIndex);
@ -552,6 +551,7 @@ class AudioPlayer {
_audioSource = source; _audioSource = source;
_broadcastSequence(); _broadcastSequence();
Duration duration; Duration duration;
if (playing) preload = true;
if (preload) { if (preload) {
duration = await load(); duration = await load();
} }
@ -660,9 +660,10 @@ class AudioPlayer {
} }
/// Tells the player to play audio as soon as an audio source is loaded and /// Tells the player to play audio as soon as an audio source is loaded and
/// ready to play. The [Future] returned by this method completes when the /// ready to play. If an audio source has been set but not preloaded, this
/// playback completes or is paused or stopped. If the player is already /// method will also initiate the loading. The [Future] returned by this
/// playing, this method completes immediately. /// method completes when the playback completes or is paused or stopped. If
/// the player is already playing, this method completes immediately.
/// ///
/// This method causes [playing] to become true, and it will remain true /// This method causes [playing] to become true, and it will remain true
/// until [pause] or [stop] is called. This means that if playback completes, /// until [pause] or [stop] is called. This means that if playback completes,
@ -677,12 +678,18 @@ class AudioPlayer {
Future<void> play() async { Future<void> play() async {
if (_disposed) return; if (_disposed) return;
if (playing) return; if (playing) return;
//_setPlatformActive(_audioSource != null); _setPlatformActive(_audioSource != null);
_playInterrupted = false; _playInterrupted = false;
// Broadcast to clients immediately, but revert to false if we fail to
// activate the audio session. This allows setAudioSource to be aware of a
// prior play request.
_playingSubject.add(true);
final audioSession = await AudioSession.instance; final audioSession = await AudioSession.instance;
if (await audioSession.setActive(true)) { if (await audioSession.setActive(true)) {
_playingSubject.add(true);
await (await _platform).play(PlayRequest()); await (await _platform).play(PlayRequest());
} else {
// Revert if we fail to activate the audio session.
_playingSubject.add(false);
} }
} }