setAudioTrack method

  1. @override
Future<void> setAudioTrack(
  1. AudioTrack track, {
  2. bool synchronized = true,
})
override

Sets the current AudioTrack for audio output.

  • Currently selected AudioTrack can be accessed using state.track.audio or stream.track.audio.
  • The list of currently available AudioTracks can be obtained accessed using state.tracks.audio or stream.tracks.audio.
  • External audio track can be loaded using AudioTrack.uri constructor.
player.setAudioTrack(
  AudioTrack.uri(
    'https://www.iandevlin.com/html5test/webvtt/v/upc-tobymanley.mp4',
    title: 'English',
    language: 'en',
  ),
);

Implementation

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

    if (track.uri) {
      await _command(
        [
          'audio-add',
          track.id,
          'select',
          track.title ?? 'external',
          track.language ?? 'auto',
        ],
      );
      state = state.copyWith(
        track: state.track.copyWith(
          audio: track,
        ),
      );
      if (!trackController.isClosed) {
        trackController.add(state.track);
      }
    } else {
      final name = 'aid'.toNativeUtf8();
      final value = track.id.toNativeUtf8();
      mpv.mpv_set_property_string(
        ctx,
        name.cast(),
        value.cast(),
      );
      calloc.free(name);
      calloc.free(value);
      state = state.copyWith(
        track: state.track.copyWith(
          audio: track,
        ),
      );
      if (!trackController.isClosed) {
        trackController.add(state.track);
      }
    }
  }

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