chatapp3-flutter/lib/shared/tools/sc_entrance_vap_svga_manager.dart
2026-04-09 21:32:23 +08:00

247 lines
7.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_svga/flutter_svga.dart';
import 'package:yumi/shared/tools/sc_path_utils.dart';
import 'package:yumi/shared/data_sources/sources/local/file_cache_manager.dart';
import 'package:tancent_vap/widgets/vap_view.dart';
import 'package:yumi/shared/tools/sc_gift_vap_svga_manager.dart';
// class EntranceVapSvgaManager {
// static EntranceVapSvgaManager? _instance;
//
// EntranceVapSvgaManager._internal();
//
// factory EntranceVapSvgaManager() =>
// _instance ??= EntranceVapSvgaManager._internal();
//
// // 使用 SCPriorityQueue 替代简单列表
// final SCPriorityQueue<SCVapTask> _taskQueue = SCPriorityQueue<SCVapTask>(
// (a, b) => b.priority.compareTo(a.priority),
// );
// VapController? _roomGiftvapController;
// SVGAAnimationController? _roomGiftsvgaController;
// bool _isPlaying = false;
// bool _isDisposed = false;
//
// // 绑定控制器
// void bindEntranceVapController(VapController vapController) {
// _isDisposed = false;
// _roomGiftvapController = vapController;
// _roomGiftvapController?.setAnimListener(
// onVideoStart: () {},
// onVideoComplete: () {
// _handleControllerState();
// },
// onFailed: (code, type, msg) {
// _handleControllerState();
// },
// );
// }
//
// void bindEntranceSvgaController(SVGAAnimationController svgaController) {
// _isDisposed = false;
// _roomGiftsvgaController = svgaController;
// _roomGiftsvgaController?.addStatusListener((AnimationStatus status) {
// if (status.isCompleted) {
// _isPlaying = false;
// _roomGiftsvgaController?.reset();
// _playNext();
// }
// });
// }
//
// // 播放任务
// void play(
// String path, {
// int priority = 0,
// Map<String, String>? customResources,
// }) {
// if (_isDisposed) return;
// final task = SCVapTask(
// path: path,
// priority: priority,
// customResources: customResources,
// );
//
// _taskQueue.add(task);
// if (!_isPlaying) {
// _playNext();
// }
// }
//
// // 播放下一个任务
// Future<void> _playNext() async {
// if (_isDisposed || _taskQueue.isEmpty || _isPlaying) return;
//
// final task = _taskQueue.removeFirst();
// _isPlaying = true;
// try {
// final pathType = SCPathUtils.getPathType(task.path);
// if (pathType == PathType.asset) {
// await _playAsset(task);
// } else if (pathType == PathType.file) {
// await _playFile(task);
// } else if (pathType == PathType.network) {
// await _playNetwork(task);
// }
// } catch (e, s) {
// print('VAP_SVGA播放失败: $e\n$s');
// _isPlaying = false;
// } finally {
// // 确保状态正确重置
// if (!_isDisposed) {
// _playNext();
// }
// }
// }
//
// // 播放资源文件
// Future<void> _playAsset(SCVapTask task) async {
// if (_isDisposed) return;
// if (SCPathUtils.getFileExtension(task.path).toLowerCase() == ".svga") {
// MovieEntity entity;
// if (SCGiftVapSvgaManager().videoItemCache.containsKey(task.path)) {
// entity = SCGiftVapSvgaManager().videoItemCache[task.path]!;
// } else {
// entity = await SVGAParser.shared.decodeFromAssets(task.path);
// SCGiftVapSvgaManager().videoItemCache[task.path] = entity;
// }
// _roomGiftsvgaController?.videoItem = entity;
// _roomGiftsvgaController?.reset();
// _roomGiftsvgaController?.forward();
// } else {
// await _roomGiftvapController?.playAsset(task.path);
// }
// }
//
// // 播放本地文件
// Future<void> _playFile(SCVapTask task) async {
// if (_isDisposed || _roomGiftvapController == null) return;
// await _roomGiftvapController!.playFile(task.path);
// }
//
// // 播放网络资源
// Future<void> _playNetwork(SCVapTask task) async {
// if (_isDisposed) return;
// if (SCPathUtils.getFileExtension(task.path).toLowerCase() == ".svga") {
// MovieEntity? entity;
// if (SCGiftVapSvgaManager().videoItemCache.containsKey(task.path)) {
// entity = SCGiftVapSvgaManager().videoItemCache[task.path]!;
// entity.autorelease = false;
// } else {
// try {
// entity = await SVGAParser.shared.decodeFromURL(task.path);
// entity.autorelease = false;
// SCGiftVapSvgaManager().videoItemCache[task.path] = entity;
// } catch (e) {
// _isPlaying = false;
// print('svga解析出错$e');
// }
// }
// if (entity != null) {
// _roomGiftsvgaController?.videoItem = entity;
// _roomGiftsvgaController?.reset();
// _roomGiftsvgaController?.forward();
// }
// } else {
// final file = await FileCacheManager.getInstance().getFile(url: task.path);
// if (file != null && !_isDisposed) {
// await _playFile(
// SCVapTask(
// path: file.path,
// priority: task.priority,
// customResources: task.customResources,
// ),
// );
// }
// }
// }
//
// // 处理控制器状态变化
// void _handleControllerState() {
// if (_roomGiftvapController != null && !_isDisposed) {
// _isPlaying = false;
// // 延迟一小段时间后播放下一个,避免状态冲突
// Future.delayed(const Duration(milliseconds: 50), _playNext);
// }
// }
//
// // 释放资源
// void dispose() {
// _isDisposed = true;
// _isPlaying = false;
// _taskQueue.clear();
// _roomGiftvapController?.stop();
// _roomGiftvapController?.dispose();
// _roomGiftvapController = null;
// _roomGiftsvgaController?.stop();
// _roomGiftsvgaController?.dispose();
// _roomGiftsvgaController = null;
// }
//
// // 清除所有任务
// void clearTasks() {
// _taskQueue.clear();
// }
//
// // 获取当前队列状态
// VapQueueStatus get queueStatus {
// return VapQueueStatus(
// isPlaying: _isPlaying,
// currentTask: _taskQueue.isNotEmpty ? _taskQueue.first : null,
// queueLength: _taskQueue.length,
// pendingTasks: List.unmodifiable(_taskQueue.unorderedElements),
// );
// }
// }
// 任务模型
class SCVapTask implements Comparable<SCVapTask> {
final String path;
final int priority; // 优先级 (数值越高越优先)
final Map<String, String>? customResources; // 自定义资源
SCVapTask({required this.path, this.priority = 0, this.customResources});
@override
int compareTo(SCVapTask other) {
// 先按优先级排序
if (priority != other.priority) {
return priority.compareTo(other.priority);
}
// 相同优先级按添加时间排序(先进先出)
return hashCode.compareTo(other.hashCode);
}
}
// 优先队列实现
class SCPriorityQueue<E> {
final List<E> _els = [];
final Comparator<E> _cmp;
SCPriorityQueue(this._cmp);
void add(E element) {
_els.add(element);
_els.sort(_cmp);
}
E removeFirst() {
if (isEmpty) throw StateError("No elements");
return _els.removeAt(0);
}
E? get first => isEmpty ? null : _els.first;
bool get isEmpty => _els.isEmpty;
bool get isNotEmpty => _els.isNotEmpty;
int get length => _els.length;
void clear() => _els.clear();
List<E> get unorderedElements => List.from(_els);
}