初始化
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:mime/mime.dart';
|
||||
|
||||
import '../track_event_manager/device_service.dart';
|
||||
import 'file_util.dart';
|
||||
|
||||
class MediaInfo {
|
||||
int? size;
|
||||
String? resolution;
|
||||
int? videoHeight;
|
||||
double? ratio;
|
||||
int? playTime;
|
||||
String? bitrate;
|
||||
String? mimeType;
|
||||
}
|
||||
|
||||
const _channel = MethodChannel(DeviceInfoService.naticeChannel);
|
||||
|
||||
/// 获取本地视频的元信息(高度/分辨率/比例/时长/比特率/大小/MIME)
|
||||
Future<MediaInfo?> getMediaInfo(String localPath) async {
|
||||
if (!FileUtil.isFileExist(localPath)) return null;
|
||||
final info = MediaInfo();
|
||||
info.videoHeight = int.tryParse(await _invokeChannel("getVideoHeight", localPath, "")) ?? 0;
|
||||
info.resolution = await _invokeChannel("getVideoResolution", localPath, "");
|
||||
info.ratio = double.tryParse(await _invokeChannel("getVideoRatio", localPath, "")) ?? 0;
|
||||
info.playTime = await _invokeChannel("getVideoDuration", localPath, 0);
|
||||
info.bitrate = await _invokeChannel("getVideoBitrate", localPath, "");
|
||||
info.size = FileUtil.getFileSize(localPath);
|
||||
info.mimeType = _getMimeType(localPath);
|
||||
return info;
|
||||
}
|
||||
|
||||
/// 统一调用原生方法,异常或类型不符时返回 fallback
|
||||
/// 注:不能只 catch PlatformException——原生 notImplemented / 没实现该 channel(iOS 就没实现这几个)
|
||||
/// 抛的是 MissingPluginException,不是 PlatformException,只兜前者等于没兜
|
||||
Future<T> _invokeChannel<T>(String method, String localPath, T fallback) async {
|
||||
try {
|
||||
final result = await _channel.invokeMethod(method, {'filePath': localPath});
|
||||
return result is T ? result : fallback;
|
||||
} catch (_) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取 MIME 类型,识别不到返回空串
|
||||
String _getMimeType(String localPath) => lookupMimeType(localPath) ?? '';
|
||||
Reference in New Issue
Block a user