AvMediaPlayer constructor

AvMediaPlayer({
  1. String? initSource,
  2. double? initVolume,
  3. double? initSpeed,
  4. bool? initLooping,
  5. bool? initAutoPlay,
  6. int? initPosition,
  7. bool? initShowSubtitle,
  8. String? initPreferredSubtitleLanguage,
  9. String? initPreferredAudioLanguage,
  10. int? initMaxBitRate,
  11. Size? initMaxResolution,
})

All parameters are optional, and can be changed later by calling the corresponding methods.

Implementation

AvMediaPlayer({
  String? initSource,
  double? initVolume,
  double? initSpeed,
  bool? initLooping,
  bool? initAutoPlay,
  int? initPosition,
  bool? initShowSubtitle,
  String? initPreferredSubtitleLanguage,
  String? initPreferredAudioLanguage,
  int? initMaxBitRate,
  Size? initMaxResolution,
}) {
  if (kDebugMode && !_detectorStarted) {
    _detectorStarted = true;
    final receivePort = ReceivePort();
    receivePort.listen((_) => _methodChannel.invokeMethod('dispose'));
    Isolate.spawn(
      (_) {},
      null,
      paused: true,
      onExit: receivePort.sendPort,
      debugName: 'AvMediaPlayer restart detector',
    );
  }
  _methodChannel.invokeMethod('create').then((value) {
    if (disposed) {
      _methodChannel.invokeMethod('dispose', value['id']);
    } else {
      subId = value['subId'];
      id.value = value['id'];
      _eventSubscription = EventChannel('av_media_player/${id.value}')
          .receiveBroadcastStream()
          .listen((event) {
        final e = event as Map;
        if (e['event'] == 'mediaInfo') {
          if (_source == e['source']) {
            loading.value = false;
            playbackState.value = PlaybackState.paused;
            mediaInfo.value = MediaInfo(
                e['duration'],
                (e['tracks'] as Map).map(
                    (k, v) => MapEntry(k as String, TrackInfo.fromMap(v))),
                _source!);
            if (autoPlay.value) {
              play();
            }
            if (_position != null) {
              seekTo(_position!);
              _position = null;
            }
          }
        } else if (e['event'] == 'videoSize') {
          if (playbackState.value != PlaybackState.closed || loading.value) {
            final width = e['width'] as double;
            final height = e['height'] as double;
            if (width != videoSize.value.width ||
                height != videoSize.value.height) {
              videoSize.value = width > 0 && height > 0
                  ? Size(e['width'], e['height'])
                  : Size.zero;
            }
          }
        } else if (e['event'] == 'playbackState') {
          playbackState.value = e['value'] == 'playing'
              ? PlaybackState.playing
              : e['value'] == 'paused'
                  ? PlaybackState.paused
                  : PlaybackState.closed;
        } else if (e['event'] == 'position') {
          if (mediaInfo.value != null) {
            position.value = e['value'] > mediaInfo.value!.duration
                ? mediaInfo.value!.duration
                : e['value'] < 0
                    ? 0
                    : e['value'];
          }
        } else if (e['event'] == 'buffer') {
          if (mediaInfo.value != null) {
            final begin = e['begin'] as int;
            final end = e['end'] as int;
            bufferRange.value = begin == 0 && end == 0
                ? BufferRange.empty
                : BufferRange(begin, end);
          }
        } else if (e['event'] == 'error') {
          // ignore errors when player is closed
          if (playbackState.value != PlaybackState.closed || loading.value) {
            _source = null;
            error.value = e['value'];
            loading.value = false;
            _close();
          }
        } else if (e['event'] == 'loading') {
          if (mediaInfo.value != null) {
            loading.value = e['value'];
          }
        } else if (e['event'] == 'seekEnd') {
          if (mediaInfo.value != null) {
            _seeking = false;
            loading.value = false;
          }
        } else if (e['event'] == 'finished') {
          if (mediaInfo.value != null) {
            if (!looping.value && mediaInfo.value!.duration != 0) {
              playbackState.value = PlaybackState.paused;
            }
            finishedTimes.value += 1;
            if (mediaInfo.value!.duration == 0) {
              playbackState.value = PlaybackState.closed;
            }
          }
        }
      });
      if (_source != null) {
        open(_source!);
      }
      if (volume.value != 1) {
        _setVolume();
      }
      if (speed.value != 1) {
        _setSpeed();
      }
      if (looping.value) {
        _setLooping();
      }
      if (maxBitRate.value > 0) {
        _setMaxBitRate();
      }
      if (maxResolution.value != Size.zero) {
        _setMaxResolution();
      }
      if (preferredAudioLanguage.value.isNotEmpty) {
        _setPreferredAudioLanguage();
      }
      if (preferredSubtitleLanguage.value.isNotEmpty) {
        _setPreferredSubtitleLanguage();
      }
      if (showSubtitle.value) {
        _setShowSubtitle();
      }
    }
  });
  _position = initPosition;
  if (initSource != null) {
    open(initSource);
  }
  if (initVolume != null) {
    setVolume(initVolume);
  }
  if (initSpeed != null) {
    setSpeed(initSpeed);
  }
  if (initLooping != null) {
    setLooping(initLooping);
  }
  if (initAutoPlay != null) {
    setAutoPlay(initAutoPlay);
  }
  if (initMaxBitRate != null) {
    setMaxBitRate(initMaxBitRate);
  }
  if (initMaxResolution != null) {
    setMaxResolution(initMaxResolution);
  }
  if (initPreferredAudioLanguage != null) {
    setPreferredAudioLanguage(initPreferredAudioLanguage);
  }
  if (initPreferredSubtitleLanguage != null) {
    setPreferredSubtitleLanguage(initPreferredSubtitleLanguage);
  }
  if (initShowSubtitle != null) {
    setShowSubtitle(initShowSubtitle);
  }
}