Add initialPosition and initialIndex parameters to load.

This commit is contained in:
Ryan Heise 2020-10-16 13:28:31 +11:00
parent 556598a663
commit f38ff383f5
9 changed files with 71 additions and 23 deletions

View File

@ -64,6 +64,8 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
private Long start; private Long start;
private Long end; private Long end;
private Long seekPos; private Long seekPos;
private long initialPos;
private Integer initialIndex;
private Result prepareResult; private Result prepareResult;
private Result playResult; private Result playResult;
private Result seekResult; private Result seekResult;
@ -185,6 +187,11 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
@Override @Override
public void onTimelineChanged(Timeline timeline, int reason) { public void onTimelineChanged(Timeline timeline, int reason) {
if (initialPos != C.TIME_UNSET || initialIndex != null) {
player.seekTo(initialIndex, initialPos);
initialIndex = null;
initialPos = C.TIME_UNSET;
}
if (reason == Player.TIMELINE_CHANGE_REASON_DYNAMIC) { if (reason == Player.TIMELINE_CHANGE_REASON_DYNAMIC) {
onItemMayHaveChanged(); onItemMayHaveChanged();
} }
@ -285,7 +292,11 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
try { try {
switch (call.method) { switch (call.method) {
case "load": case "load":
load(getAudioSource(request.get("audioSource")), result); Long initialPosition = getLong(request.get("initialPosition"));
Integer initialIndex = (Integer)request.get("initialIndex");
load(getAudioSource(request.get("audioSource")),
initialPosition == null ? C.TIME_UNSET : initialPosition / 1000,
initialIndex, result);
break; break;
case "play": case "play":
play(result); play(result);
@ -316,7 +327,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
case "seek": case "seek":
Long position = getLong(request.get("position")); Long position = getLong(request.get("position"));
Integer index = (Integer)request.get("index"); Integer index = (Integer)request.get("index");
seek(position == null ? C.TIME_UNSET : position / 1000, result, index); seek(position == null ? C.TIME_UNSET : position / 1000, index, result);
break; break;
case "concatenatingInsertAll": case "concatenatingInsertAll":
concatenating(request.get("id")) concatenating(request.get("id"))
@ -492,7 +503,9 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
return new DefaultDataSourceFactory(context, httpDataSourceFactory); return new DefaultDataSourceFactory(context, httpDataSourceFactory);
} }
private void load(final MediaSource mediaSource, final Result result) { private void load(final MediaSource mediaSource, final long initialPosition, final Integer initialIndex, final Result result) {
this.initialPos = initialPosition;
this.initialIndex = initialIndex;
switch (processingState) { switch (processingState) {
case none: case none:
break; break;
@ -662,7 +675,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud
player.setShuffleModeEnabled(enabled); player.setShuffleModeEnabled(enabled);
} }
public void seek(final long position, final Result result, final Integer index) { public void seek(final long position, final Integer index, final Result result) {
if (processingState == ProcessingState.none || processingState == ProcessingState.loading) { if (processingState == ProcessingState.none || processingState == ProcessingState.loading) {
result.success(new HashMap<String, Object>()); result.success(new HashMap<String, Object>());
return; return;

View File

@ -120,16 +120,16 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.5.4" version: "0.5.4+1"
just_audio_platform_interface: just_audio_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: just_audio_platform_interface path: "../../just_audio_platform_interface"
url: "https://pub.dartlang.org" relative: true
source: hosted source: path
version: "1.1.0" version: "1.1.0"
just_audio_web: just_audio_web:
dependency: "direct main" dependency: transitive
description: description:
path: "../../just_audio_web" path: "../../just_audio_web"
relative: true relative: true

View File

@ -442,20 +442,25 @@ class AudioPlayer {
Future<Duration> setAsset(String assetPath) => Future<Duration> setAsset(String assetPath) =>
load(AudioSource.uri(Uri.parse('asset:///$assetPath'))); load(AudioSource.uri(Uri.parse('asset:///$assetPath')));
/// Loads audio from an [AudioSource] and completes when the audio is ready /// Loads audio from an [AudioSource] and completes when the audio is ready to
/// to play with the duration of that audio, or null if the duration is unknown. /// play with the duration of that audio, or null if the duration is unknown.
/// Optionally specify [initialPosition] and [initialIndex] to seek to an
/// initial position within a particular item (defaulting to position zero of
/// the first item).
/// ///
/// This method throws: /// This method throws:
/// ///
/// * [PlayerException] if the audio source was unable to be loaded. /// * [PlayerException] if the audio source was unable to be loaded.
/// * [PlayerInterruptedException] if another call to [load] happened before /// * [PlayerInterruptedException] if another call to [load] happened before
/// this call completed. /// this call completed.
Future<Duration> load(AudioSource source) async { Future<Duration> load(AudioSource source,
{Duration initialPosition, int initialIndex}) async {
if (_disposed) return null; if (_disposed) return null;
try { try {
_audioSource = source; _audioSource = source;
_broadcastSequence(); _broadcastSequence();
final duration = await _load(source); final duration = await _load(source,
initialPosition: initialPosition, initialIndex: initialIndex);
// Wait for loading state to pass. // Wait for loading state to pass.
await processingStateStream await processingStateStream
.firstWhere((state) => state != ProcessingState.loading); .firstWhere((state) => state != ProcessingState.loading);
@ -474,7 +479,8 @@ class AudioPlayer {
_audioSources[source._id] = source; _audioSources[source._id] = source;
} }
Future<Duration> _load(AudioSource source) async { Future<Duration> _load(AudioSource source,
{Duration initialPosition, int initialIndex}) async {
try { try {
if (!kIsWeb && source._requiresHeaders) { if (!kIsWeb && source._requiresHeaders) {
if (_proxy == null) { if (_proxy == null) {
@ -484,7 +490,11 @@ class AudioPlayer {
} }
await source._setup(this); await source._setup(this);
_durationFuture = (await _platform) _durationFuture = (await _platform)
.load(LoadRequest(audioSourceMessage: source._toMessage())) .load(LoadRequest(
audioSourceMessage: source._toMessage(),
initialPosition: initialPosition,
initialIndex: initialIndex,
))
.then((response) => response.duration); .then((response) => response.duration);
final duration = await _durationFuture; final duration = await _durationFuture;
_durationSubject.add(duration); _durationSubject.add(duration);

View File

@ -110,10 +110,17 @@ packages:
just_audio_platform_interface: just_audio_platform_interface:
dependency: "direct main" dependency: "direct main"
description: description:
name: just_audio_platform_interface path: "../just_audio_platform_interface"
url: "https://pub.dartlang.org" relative: true
source: hosted source: path
version: "1.1.0" version: "1.1.0"
just_audio_web:
dependency: "direct main"
description:
path: "../just_audio_web"
relative: true
source: path
version: "0.1.0"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:

View File

@ -8,8 +8,12 @@ environment:
flutter: ">=1.12.13+hotfix.5" flutter: ">=1.12.13+hotfix.5"
dependencies: dependencies:
just_audio_platform_interface: ^1.1.0 # just_audio_platform_interface: ^1.2.0
just_audio_web: ^0.1.0 # just_audio_web: ^0.1.0
just_audio_platform_interface:
path: ../just_audio_platform_interface
just_audio_web:
path: ../just_audio_web
audio_session: ^0.0.9 audio_session: ^0.0.9
rxdart: ^0.24.1 rxdart: ^0.24.1
path: ^1.6.4 path: ^1.6.4

View File

@ -1,3 +1,7 @@
## 1.2.0
- Add initialPosition and initialIndex to LoadRequest.
## 1.1.0 ## 1.1.0
- Player is now disposed via JustAudioPlatform.disposePlayer(). - Player is now disposed via JustAudioPlatform.disposePlayer().

View File

@ -297,11 +297,19 @@ class DisposePlayerResponse {
/// audio source. /// audio source.
class LoadRequest { class LoadRequest {
final AudioSourceMessage audioSourceMessage; final AudioSourceMessage audioSourceMessage;
final Duration initialPosition;
final int initialIndex;
LoadRequest({@required this.audioSourceMessage}); LoadRequest({
@required this.audioSourceMessage,
this.initialPosition,
this.initialIndex,
});
Map<dynamic, dynamic> toMap() => { Map<dynamic, dynamic> toMap() => {
'audioSource': audioSourceMessage.toMap(), 'audioSource': audioSourceMessage.toMap(),
'initialPosition': initialPosition?.inMicroseconds,
'initialIndex': initialIndex,
}; };
} }

View File

@ -3,7 +3,7 @@ description: A common platform interface for the just_audio plugin.
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_platform_interface homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a # NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.1.0 version: 1.2.0
dependencies: dependencies:
flutter: flutter:

View File

@ -11,7 +11,9 @@ flutter:
fileName: just_audio_web.dart fileName: just_audio_web.dart
dependencies: dependencies:
just_audio_platform_interface: ^1.1.0 # just_audio_platform_interface: ^1.2.0
just_audio_platform_interface:
path: ../just_audio_platform_interface
flutter: flutter:
sdk: flutter sdk: flutter
flutter_web_plugins: flutter_web_plugins: