seek method

  1. @override
Future<void> seek(
  1. Duration duration, {
  2. bool synchronized = true,
})
override

Seeks the currently playing Media in the Player by specified Duration.

Implementation

@override
Future<void> seek(Duration duration, {bool synchronized = true}) {
  Future<void> function() async {
    if (disposed) {
      throw AssertionError('[Player] has been disposed');
    }
    await waitForPlayerInitialization;
    await waitForVideoControllerInitializationIfAttached;

    // Raw `mpv_command` calls cause crash on Windows.
    final args = [
      'seek',
      (duration.inMilliseconds / 1000).toStringAsFixed(4),
      'absolute',
    ].join(' ').toNativeUtf8();
    mpv.mpv_command_string(
      ctx,
      args.cast(),
    );
    calloc.free(args);

    // It is self explanatory that PlayerState.completed & PlayerStreams.completed must enter the false state if seek is called. Typically after EOF.
    // https://github.com/media-kit/media-kit/issues/221
    state = state.copyWith(completed: false);
    if (!completedController.isClosed) {
      completedController.add(false);
    }
  }

  if (synchronized) {
    return lock.synchronized(function);
  } else {
    return function();
  }
}