killapp/Assets/Scripts/UI/Pages/VideoPage/VideoPageCtrl.cs
“虞渠成” 6e385e2bab refactor(iOS/视频页): 清理无用调试日志并修复类型转换问题
1. 移除VideoPageCtrl中多余的Debug.Log调试输出
2. 为iOS平台的FFmpegKit信号忽略调用添加正确的枚举强转
2026-09-08 15:08:24 +08:00

354 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Kill.UI.Components;
using Kill.UI;
using System.Threading.Tasks;
using System;
using Kill.Network;
using Kill.Managers;
using UnityEngine.Video;
using System.IO;
namespace Kill.UI.Pages
{
public class VideoPageCtrl : MonoBehaviour
{
public class Videodata
{
public string videoUrl;
public string coverUrl;
public DateTime time;
}
public class KillData
{
public DateTime time;
public float angle;
public float dis;
}
public Button backBtn;
public VerticalScrollLoader scrollLoader;
public VideoUIItem videoUIItemPrefab;
public Transform content;
int totalCount=0;
List<GameObject> videoList = new List<GameObject>();
private int currentPage = 1;
private bool isLoading = false;
private bool hasMoreData = true;
public GameObject noTip;
void Start()
{
backBtn.onClick.RemoveAllListeners();
backBtn.onClick.AddListener(Back);
UIManager.Instance.RegisterBackAction(Back);
ChangeType(0);
}
public Button[] changeTypeButtons;
public void ChangeType(int type)
{
if(videoList!=null)
{
foreach(var g in videoList)
{
Destroy(g);
}
videoList.Clear();
}
videoList=new List<GameObject>();
changeTypeButtons[type].interactable = false;
changeTypeButtons[type].transform.Find("选中").gameObject.SetActive(true);
changeTypeButtons[1-type].interactable = true;
changeTypeButtons[1-type].transform.Find("选中").gameObject.SetActive(false);
if (type == 0)
{
// 设置滚动加载阈值
if (scrollLoader != null)
{
scrollLoader.onLoadTriggered.AddListener(OnScrollToLoadKill);
scrollLoader.ResetLoader();
}
currentPage = 1;
hasMoreData = true;
totalCount=0;
GetKillDataList();
}
else
{
// 设置滚动加载阈值
if (scrollLoader != null)
{
scrollLoader.onLoadTriggered.AddListener(OnScrollToLoadVideo);
scrollLoader.ResetLoader();
}
currentPage = 1;
hasMoreData = true;
totalCount=0;
GetVideoList();
}
}
public void Back()
{
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
public async Task GetVideoList()
{
isLoading = true;
LoadingUI.Show();
string url = $"/api/v1/stats/device/videos?device_sn={DataManager.Instance.selectedDevice.ble_mac}&page={currentPage}&page_size=30";
var response = await NetworkCtrl.Instance.Get<DeviceVideoDataListResponse>(url);
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
if(data.data.total>0)
{
noTip.SetActive(false);
}
else
{
noTip.SetActive(true);
}
isLoading = false;
if (data.data.list != null && data.data.list.Count > 0)
{
foreach (var item in data.data.list)
{
Videodata vd = new Videodata()
{
time = item.record_time,
videoUrl = item.video_url,
coverUrl=item.cover_url
};
CreateVideoItem(vd);
totalCount++;
}
if(totalCount>=data.data.total)
{
hasMoreData = false;
scrollLoader?.SetAllDataLoaded();
}
else
{
hasMoreData = true;
scrollLoader?.OnLoadComplete();
}
}
},
onError: null
);
}
public async Task GetKillDataList()
{
isLoading = true;
LoadingUI.Show();
string url = $"/api/v1/stats/device/records?device_sn={DataManager.Instance.selectedDevice.ble_mac}&type=1&page={currentPage}&page_size=30";
var response = await NetworkCtrl.Instance.Get<RecordDataListResponse>(url);
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
if(data.data.total>0)
{
noTip.SetActive(false);
}
else
{
noTip.SetActive(true);
}
isLoading = false;
if (data.data.records != null && data.data.records.Count > 0)
{
foreach (var item in data.data.records)
{
KillData kd = new KillData()
{
time = item.time,
angle = item.angle,
dis=item.distance
};
CreateKillItem(kd);
totalCount++;
}
if(totalCount>=data.data.total)
{
hasMoreData = false;
scrollLoader?.SetAllDataLoaded();
}
else
{
hasMoreData = true;
scrollLoader?.OnLoadComplete();
}
}
},
onError: null
);
}
/// <summary>
/// 滚动到指定位置加载新消息
/// </summary>
private void OnScrollToLoadKill()
{
Debug.Log("加载新记录");
if (isLoading || !hasMoreData) return;
currentPage++;
GetKillDataList();
}
private void OnScrollToLoadVideo()
{
if (isLoading || !hasMoreData) return;
currentPage++;
GetVideoList();
}
/// <summary>
/// 创建视频UI
/// </summary>
private void CreateVideoItem(Videodata data)
{
if (videoUIItemPrefab == null || content == null) return;
var item = Instantiate(videoUIItemPrefab, content);
var comp = item.GetComponent<VideoUIItem>();
comp.Init(data.time, data.coverUrl);
comp.SetupVideo(data.videoUrl,
onDownload: () => OnItemDownload(comp),
onPlay: () => OnItemPlay(comp));
//item.GetComponent<Button>().onClick.AddListener(() => OnItemRowClick(comp));
videoList.Add(item.gameObject);
}
/// <summary>
/// 创建灭蚊记录UI
/// </summary>
/// <param name="data"></param>
private void CreateKillItem(KillData data)
{
if (videoUIItemPrefab == null || content == null) return;
var item = Instantiate(videoUIItemPrefab, content);
item.GetComponent<VideoUIItem>().Init(data.time, "",data.angle,data.dis);
videoList.Add(item.gameObject);
}
public VideoCtrl videoCtrl;
public Button downloadBtn;
/// <summary>用本地 mp4 打开播放器面板,并把"保存到相册"按钮绑定到该本地文件</summary>
private void OpenPlayer(string path)
{
if (videoCtrl == null) { ToastUI.Show("100332"); return; }
videoCtrl.transform.parent.gameObject.SetActive(true);
videoCtrl.Init(path, null);
if (downloadBtn != null)
{
downloadBtn.onClick.RemoveAllListeners();
downloadBtn.onClick.AddListener(() => SaveVideoToGallery(path, Path.GetFileName(path)));
}
}
public void CloseVideo()
{
videoCtrl.transform.parent.gameObject.SetActive(false);
videoCtrl.Stop();
}
// ============ 列表项:下载 / 播放 逻辑 ============
/// <summary>点 item 下载按钮:仅下载(+h264 转码),完成后刷新按钮为"播放"</summary>
public void OnItemDownload(VideoUIItem item)
{
if (item == null || string.IsNullOrEmpty(item.videoUrl)) return;
item.ShowDownloading(true);
new Kill.Video.VideoDownloadPlayService().DownloadConvertAndPlay(
item.videoUrl,
onDownloadProgress: p => item.UpdateDownloadProgress(false, p),
onConvertStart: null,
onComplete: (success, path) =>
{
item.UpdateDownloadProgress(true, null);
if (success) item.RefreshButtons();
else ToastUI.Show("100258");
});
}
/// <summary>点 item 播放按钮:播本地缓存;未缓存则先下载+转码再播</summary>
public void OnItemPlay(VideoUIItem item)
{
if (item == null || string.IsNullOrEmpty(item.videoUrl)) return;
string path = Kill.Video.VideoCacheManager.GetCachedMp4Path(item.videoUrl);
if (File.Exists(path) && new FileInfo(path).Length > 0)
OpenPlayer(path);
else
OnItemDownloadAndPlay(item);
}
/// <summary>点击 item 整行:已下载直接播,未下载先下载(+转码)再播</summary>
public void OnItemRowClick(VideoUIItem item)
{
if (item == null || string.IsNullOrEmpty(item.videoUrl)) return;
if (Kill.Video.VideoCacheManager.IsCached(item.videoUrl))
OpenPlayer(Kill.Video.VideoCacheManager.GetCachedMp4Path(item.videoUrl));
else
OnItemDownloadAndPlay(item);
}
private void OnItemDownloadAndPlay(VideoUIItem item)
{
if (item == null || string.IsNullOrEmpty(item.videoUrl)) return;
item.ShowDownloading(true);
new Kill.Video.VideoDownloadPlayService().DownloadConvertAndPlay(
item.videoUrl,
onDownloadProgress: p => item.UpdateDownloadProgress(false, p),
onConvertStart: null,
onComplete: (success, path) =>
{
item.UpdateDownloadProgress(true, null);
item.RefreshButtons();
if (success && !string.IsNullOrEmpty(path) && File.Exists(path))
OpenPlayer(path);
else
ToastUI.Show("100258");
});
}
/// <summary>
/// 保存视频到系统相册
/// </summary>
private void SaveVideoToGallery(string filePath, string fileName)
{
#if UNITY_ANDROID || UNITY_IOS
NativeGallery.SaveVideoToGallery(filePath, "photonmatrix", fileName, (success, path) =>
{
if (success)
{
ToastUI.Show("100257");
Debug.Log($"[VideoPageCtrl] 视频已保存到相册: {path}");
}
else
{
ToastUI.Show("100258");
Debug.LogError($"[VideoPageCtrl] 保存视频到相册失败: {filePath}");
}
});
#endif
}
}
}