init method
Future<int>
init(
- dynamic token, {
- required P2pConfig config,
- void infoListener()?,
- SegmentIdGenerator? segmentIdGenerator,
- bool bufferedDurationGeneratorEnable = false,
override
Create a new instance with token and the specified config.
Implementation
Future<int> init(
token, {
required P2pConfig config,
void Function(Map<String, dynamic>)? infoListener,
SegmentIdGenerator? segmentIdGenerator, // 给SDK提供segmentId
bool bufferedDurationGeneratorEnable = false, // 是否可以给SDK提供缓冲前沿到当前播放时间的差值
}) async {
_bufferedDurationGeneratorEnable = bufferedDurationGeneratorEnable;
final int? success = await _channel.invokeMethod('init', {
'token': token,
'config': config.toMap,
'enableSegmentIdGenerator': segmentIdGenerator != null,
'enableBufferedDurationGenerator': bufferedDurationGeneratorEnable,
});
if (infoListener != null) {
await _channel.invokeMethod('startListen');
}
_channel.setMethodCallHandler((call) async {
if (call.method == 'info') {
var map = Map<String, dynamic>.from(call.arguments);
infoListener?.call(map);
} else if (call.method == 'segmentId') {
/// SegmentIdGenerator
var data = SafeMap(call.arguments);
return {
'result': (segmentIdGenerator ?? defaultSegmentIdGenerator).call(
data['streamId'].string ?? "",
data['sn'].intValue ?? 0,
data['segmentUrl'].string ?? "",
data['range'].string,
) ??
call.arguments['url'],
};
} else if (call.method == 'bufferedDuration') {
var duration = _bufferedDurationGenerator?.call();
return {'result': duration?.inSeconds ?? -1};
}
return {"success": true};
});
if (success == null) {
throw 'Not Avaliable Result: $success. Init fail.';
}
return success;
}