diff --git a/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java index dcaee0a..77ea8f5 100644 --- a/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java +++ b/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java @@ -64,6 +64,8 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud private Long start; private Long end; private Long seekPos; + private long initialPos; + private Integer initialIndex; private Result prepareResult; private Result playResult; private Result seekResult; @@ -185,6 +187,11 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud @Override 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) { onItemMayHaveChanged(); } @@ -285,7 +292,11 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud try { switch (call.method) { 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; case "play": play(result); @@ -316,7 +327,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud case "seek": Long position = getLong(request.get("position")); 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; case "concatenatingInsertAll": concatenating(request.get("id")) @@ -492,7 +503,9 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud 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) { case none: break; @@ -662,7 +675,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Aud 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) { result.success(new HashMap()); return; diff --git a/just_audio/example/pubspec.lock b/just_audio/example/pubspec.lock index d0aba99..0958f97 100644 --- a/just_audio/example/pubspec.lock +++ b/just_audio/example/pubspec.lock @@ -120,16 +120,16 @@ packages: path: ".." relative: true source: path - version: "0.5.4" + version: "0.5.4+1" just_audio_platform_interface: dependency: transitive description: - name: just_audio_platform_interface - url: "https://pub.dartlang.org" - source: hosted + path: "../../just_audio_platform_interface" + relative: true + source: path version: "1.1.0" just_audio_web: - dependency: "direct main" + dependency: transitive description: path: "../../just_audio_web" relative: true diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index 8eb60c5..f175cb6 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -442,20 +442,25 @@ class AudioPlayer { Future setAsset(String assetPath) => load(AudioSource.uri(Uri.parse('asset:///$assetPath'))); - /// Loads audio from an [AudioSource] and completes when the audio is ready - /// to play with the duration of that audio, or null if the duration is unknown. + /// Loads audio from an [AudioSource] and completes when the audio is ready to + /// 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: /// /// * [PlayerException] if the audio source was unable to be loaded. /// * [PlayerInterruptedException] if another call to [load] happened before /// this call completed. - Future load(AudioSource source) async { + Future load(AudioSource source, + {Duration initialPosition, int initialIndex}) async { if (_disposed) return null; try { _audioSource = source; _broadcastSequence(); - final duration = await _load(source); + final duration = await _load(source, + initialPosition: initialPosition, initialIndex: initialIndex); // Wait for loading state to pass. await processingStateStream .firstWhere((state) => state != ProcessingState.loading); @@ -474,7 +479,8 @@ class AudioPlayer { _audioSources[source._id] = source; } - Future _load(AudioSource source) async { + Future _load(AudioSource source, + {Duration initialPosition, int initialIndex}) async { try { if (!kIsWeb && source._requiresHeaders) { if (_proxy == null) { @@ -484,7 +490,11 @@ class AudioPlayer { } await source._setup(this); _durationFuture = (await _platform) - .load(LoadRequest(audioSourceMessage: source._toMessage())) + .load(LoadRequest( + audioSourceMessage: source._toMessage(), + initialPosition: initialPosition, + initialIndex: initialIndex, + )) .then((response) => response.duration); final duration = await _durationFuture; _durationSubject.add(duration); diff --git a/just_audio/pubspec.lock b/just_audio/pubspec.lock index 572fbb7..86cf674 100644 --- a/just_audio/pubspec.lock +++ b/just_audio/pubspec.lock @@ -110,10 +110,17 @@ packages: just_audio_platform_interface: dependency: "direct main" description: - name: just_audio_platform_interface - url: "https://pub.dartlang.org" - source: hosted + path: "../just_audio_platform_interface" + relative: true + source: path version: "1.1.0" + just_audio_web: + dependency: "direct main" + description: + path: "../just_audio_web" + relative: true + source: path + version: "0.1.0" matcher: dependency: transitive description: diff --git a/just_audio/pubspec.yaml b/just_audio/pubspec.yaml index a288ef7..bc3a30d 100644 --- a/just_audio/pubspec.yaml +++ b/just_audio/pubspec.yaml @@ -8,8 +8,12 @@ environment: flutter: ">=1.12.13+hotfix.5" dependencies: - just_audio_platform_interface: ^1.1.0 - just_audio_web: ^0.1.0 + # just_audio_platform_interface: ^1.2.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 rxdart: ^0.24.1 path: ^1.6.4 diff --git a/just_audio_platform_interface/CHANGELOG.md b/just_audio_platform_interface/CHANGELOG.md index c560d12..6575114 100644 --- a/just_audio_platform_interface/CHANGELOG.md +++ b/just_audio_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.0 + +- Add initialPosition and initialIndex to LoadRequest. + ## 1.1.0 - Player is now disposed via JustAudioPlatform.disposePlayer(). diff --git a/just_audio_platform_interface/lib/just_audio_platform_interface.dart b/just_audio_platform_interface/lib/just_audio_platform_interface.dart index 96c6188..449a6d7 100644 --- a/just_audio_platform_interface/lib/just_audio_platform_interface.dart +++ b/just_audio_platform_interface/lib/just_audio_platform_interface.dart @@ -297,11 +297,19 @@ class DisposePlayerResponse { /// audio source. class LoadRequest { final AudioSourceMessage audioSourceMessage; + final Duration initialPosition; + final int initialIndex; - LoadRequest({@required this.audioSourceMessage}); + LoadRequest({ + @required this.audioSourceMessage, + this.initialPosition, + this.initialIndex, + }); Map toMap() => { 'audioSource': audioSourceMessage.toMap(), + 'initialPosition': initialPosition?.inMicroseconds, + 'initialIndex': initialIndex, }; } diff --git a/just_audio_platform_interface/pubspec.yaml b/just_audio_platform_interface/pubspec.yaml index 6ea9145..688807f 100644 --- a/just_audio_platform_interface/pubspec.yaml +++ b/just_audio_platform_interface/pubspec.yaml @@ -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 # 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 -version: 1.1.0 +version: 1.2.0 dependencies: flutter: diff --git a/just_audio_web/pubspec.yaml b/just_audio_web/pubspec.yaml index 1717fe4..c5a9943 100644 --- a/just_audio_web/pubspec.yaml +++ b/just_audio_web/pubspec.yaml @@ -11,7 +11,9 @@ flutter: fileName: just_audio_web.dart 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: sdk: flutter flutter_web_plugins: