stop method

  1. @override
Future<void> stop({
  1. bool synchronized = true,
})
override

Stops the Player. Unloads the current Media or Playlist from the Player. This method is similar to dispose but does not release the resources & Player is still usable.

Implementation

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

    isShuffleEnabled = false;
    isPlayingStateChangeAllowed = false;
    isBufferingStateChangeAllowed = false;

    final commands = [
      'stop',
      'playlist-clear',
      'playlist-play-index none',
    ];
    for (final command in commands) {
      final args = command.toNativeUtf8();
      mpv.mpv_command_string(
        ctx,
        args.cast(),
      );
      calloc.free(args);
    }

    // Reset the remaining attributes.
    state = PlayerState().copyWith(
      volume: state.volume,
      rate: state.rate,
      pitch: state.pitch,
      playlistMode: state.playlistMode,
      audioDevice: state.audioDevice,
      audioDevices: state.audioDevices,
    );
    if (!playlistController.isClosed) {
      playlistController.add(Playlist([]));
    }
    if (!playingController.isClosed) {
      playingController.add(false);
    }
    if (!completedController.isClosed) {
      completedController.add(false);
    }
    if (!positionController.isClosed) {
      positionController.add(Duration.zero);
    }
    if (!durationController.isClosed) {
      durationController.add(Duration.zero);
    }
    // if (!volumeController.isClosed) {
    //   volumeController.add(0.0);
    // }
    // if (!rateController.isClosed) {
    //   rateController.add(0.0);
    // }
    // if (!pitchController.isClosed) {
    //   pitchController.add(0.0);
    // }
    if (!bufferingController.isClosed) {
      bufferingController.add(false);
    }
    if (!bufferController.isClosed) {
      bufferController.add(Duration.zero);
    }
    // if (!playlistModeController.isClosed) {
    //   playlistModeController.add(PlaylistMode.none);
    // }
    if (!audioParamsController.isClosed) {
      audioParamsController.add(const AudioParams());
    }
    if (!videoParamsController.isClosed) {
      videoParamsController.add(const VideoParams());
    }
    if (!audioBitrateController.isClosed) {
      audioBitrateController.add(null);
    }
    // if (!audioDeviceController.isClosed) {
    //   audioDeviceController.add(AudioDevice.auto());
    // }
    // if (!audioDevicesController.isClosed) {
    //   audioDevicesController.add([AudioDevice.auto()]);
    // }
    if (!trackController.isClosed) {
      trackController.add(Track());
    }
    if (!tracksController.isClosed) {
      tracksController.add(Tracks());
    }
    if (!widthController.isClosed) {
      widthController.add(null);
    }
    if (!heightController.isClosed) {
      heightController.add(null);
    }
    if (!subtitleController.isClosed) {
      subtitleController.add(['', '']);
    }
  }

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