createPlayer method

Future<String> createPlayer({
  1. String? vid,
  2. bool preCreated = false,
  3. bool useDrm = false,
})

Create a Native player, it is recommended to call this first, immediately after the constructor.

Implementation

Future<String> createPlayer({String? vid, bool preCreated = false, bool useDrm = false}) async {
  TTFLogger.i(_logTag, 'createPlayer: hashCode: $hashCode');
  if (_playerIsCreated) {
    TTFLogger.i(_logTag, 'createPlayer didCreated before: hashCode: $hashCode');
    return "";
  }
  engineInstanceMethodChannel.setMethodCallHandler((MethodCall call) async {
    switch (call.method) {
      case 'seekCompleted':
        int seekCount = call.arguments['seekSerialNo'];
        bool success = call.arguments['success'];
        final callbackId = '${hashCode}_$seekCount';
        TTFLogger.i(_logTag, 'seekCompleted: $callbackId');
        if (_seekCompletedCallbacks.containsKey(hashCode)) {
          _seekCompletedCallbacks[hashCode]?[callbackId]?.call(success);
          _seekCompletedCallbacks[hashCode]?.remove(callbackId);
        }
        break;
      case 'seekRenderCompleted':
        int seekCount = call.arguments['seekSerialNo'];
        final callbackId = '${hashCode}_$seekCount';
        TTFLogger.i(_logTag, 'seekRenderCompleted: $callbackId');
        if (_seekRenderCompletedCallbacks.containsKey(hashCode)) {
          _seekRenderCompletedCallbacks[hashCode]?[callbackId]?.call();
          _seekRenderCompletedCallbacks[hashCode]?.remove(callbackId);
        }
        break;
      case 'getHlsStreamInfos':
        TTFLogger.i(_logTag, 'getHlsStreamInfos');
        String json = call.arguments;
        Map<String, dynamic> decodedJson = jsonDecode(json);
        TTMasterPlaylist playlist = TTMasterPlaylist.fromJson(decodedJson);
        if (_getHlsStreamInfosCallbacks.containsKey(hashCode)) {
          _getHlsStreamInfosCallbacks[hashCode]!.call(playlist);
        }
        break;
      case 'selectHlsVideoStream':
        if (_selectHlsVideoStreamCallbacks.containsKey(hashCode)) {
          int? res = _selectHlsVideoStreamCallbacks[hashCode]!.call();
          return res;
        }
        break;
      case 'selectHlsRendition':
        int variantIndex = call.arguments;
        if (_selectHlsRenditionCallbacks.containsKey(hashCode)) {
          int? res = _selectHlsRenditionCallbacks[hashCode]!.call(variantIndex);
          return res;
        }
        break;
      case 'switchHlsBitrate':
        int bitrate = call.arguments;
        if (_hlsStreamSwitchCallbacks.containsKey(hashCode)) {
          _hlsStreamSwitchCallbacks[hashCode]!.call(bitrate);
        }
        break;
      case 'resolutionConfigCompletion':
        bool success = call.arguments['success'];
        int resValue = call.arguments['completeResolution'];
        TTVideoEngineResolutionType resolutionType = TTVideoEngineResolutionType.transFromValue(resValue);
        resolutionConfigCompletion!.call(success, resolutionType);
        break;
      default:
        break;
    }
  });

  String coreHashHex = await engineStaticMethodChannel
      .invokeMethod('createPlayer', {'hashCode': hashCode, 'vid': vid, 'preCreated': preCreated, 'useDrm':useDrm});
  _playerIsCreated = true;
  _subscription = engineInstanceEventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
  return coreHashHex;
}