AVMediaPlayer constructor

AVMediaPlayer({
  1. String? initSource,
  2. double? initVolume,
  3. double? initSpeed,
  4. bool? initLooping,
  5. bool? initAutoPlay,
  6. int? initPosition,
})

All the 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,
}) {
  _methodChannel.invokeMethod('create').then((value) {
    id.value = value as int;
    _eventSubscription = EventChannel('avMediaPlayer/${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'], _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'];
          mediaInfo.value = null;
          videoSize.value = Size.zero;
          position.value = 0;
          bufferRange.value = BufferRange.empty;
          finishedTimes.value = 0;
          loading.value = false;
          playbackState.value = PlaybackState.closed;
        }
      } else if (e['event'] == 'loading') {
        if (mediaInfo.value != null) {
          loading.value = e['value'];
        }
      } else if (e['event'] == 'seekEnd') {
        if (mediaInfo.value != null) {
          _seeked = false;
          loading.value = false;
        }
      } else if (e['event'] == 'finished') {
        if (mediaInfo.value != null) {
          if (!looping.value && mediaInfo.value!.duration != 0) {
            position.value = 0;
            bufferRange.value = BufferRange.empty;
            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) {
      _methodChannel.invokeMethod('setVolume', {
        'id': value,
        'value': volume.value,
      });
    }
    if (speed.value != 1) {
      _methodChannel.invokeMethod('setSpeed', {
        'id': value,
        'value': speed.value,
      });
    }
    if (looping.value) {
      _methodChannel.invokeMethod('setLooping', {
        'id': value,
        'value': true,
      });
    }
  });
  _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);
  }
}