Skip to content

Commit 7e4b610

Browse files
committed
update 本地路径视频示例优化
1 parent db1c5ec commit 7e4b610

File tree

2 files changed

+65
-346
lines changed

2 files changed

+65
-346
lines changed

Demo/API_V2/Assets/API/Video/PlayLocalFileVideo.cs

+64-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class PlayLocalFileVideo : MonoBehaviour
1010
{
1111
public VideoPlayer videoPlayer;
12+
private string localVideoPath;
1213

1314
private void Awake()
1415
{
@@ -22,10 +23,51 @@ private void Awake()
2223
// 配置VideoPlayer
2324
videoPlayer.playOnAwake = false;
2425
videoPlayer.isLooping = false;
26+
27+
// 设置本地视频路径
28+
localVideoPath = WX.env.USER_DATA_PATH + "/video.mp4";
29+
}
30+
31+
private void CheckAndPlayVideo()
32+
{
33+
// 获取文件系统管理器
34+
var fs = WX.GetFileSystemManager();
35+
36+
// 使用微信文件系统的方法检查文件是否存在
37+
fs.Access(new AccessParam()
38+
{
39+
path = localVideoPath,
40+
success = (res) =>
41+
{
42+
Debug.Log("本地视频文件已存在,直接播放");
43+
// 文件存在,直接播放
44+
LoadAndPlayVideo(localVideoPath);
45+
},
46+
fail = (res) =>
47+
{
48+
Debug.Log("本地视频文件不存在,需要下载: " + res.errMsg);
49+
// 文件不存在,需要下载
50+
DownloadFileVideo();
51+
}
52+
});
2553
}
2654

2755
private void DownloadFileVideo()
2856
{
57+
WX.ShowLoading(new ShowLoadingOption()
58+
{
59+
title = "视频下载中...",
60+
mask = true,
61+
success = (res) =>
62+
{
63+
Debug.Log("Loading indicator shown successfully");
64+
},
65+
fail = (res) =>
66+
{
67+
Debug.Log("Failed to show loading indicator: " + res.errMsg);
68+
},
69+
});
70+
2971
WX.DownloadFile(new DownloadFileOption()
3072
{
3173
url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20190812/video.mp4",
@@ -36,17 +78,35 @@ private void DownloadFileVideo()
3678
{
3779
Debug.Log(res.tempFilePath);
3880
var fs = WX.GetFileSystemManager();
39-
var filePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/video.mp4");
81+
var filePath = fs.SaveFileSync(res.tempFilePath, localVideoPath);
4082
LoadAndPlayVideo(filePath);
4183
}
4284
},
4385
fail = (res) =>
4486
{
45-
Debug.Log("WX.DownloadFile fail");
87+
Debug.Log("WX.DownloadFile fail: " + res.errMsg);
88+
HideLoadingIndicator();
4689
},
4790
complete = (res) =>
4891
{
4992
Debug.Log("WX.DownloadFile complete");
93+
HideLoadingIndicator();
94+
}
95+
});
96+
}
97+
98+
// 隐藏加载提示的方法
99+
private void HideLoadingIndicator()
100+
{
101+
WX.HideLoading(new HideLoadingOption()
102+
{
103+
success = (res) =>
104+
{
105+
Debug.Log("Loading indicator hidden successfully");
106+
},
107+
fail = (res) =>
108+
{
109+
Debug.LogError("Failed to hide loading indicator: " + res.errMsg);
50110
}
51111
});
52112
}
@@ -78,7 +138,8 @@ private void CleanupVideo()
78138

79139
private void Start()
80140
{
81-
DownloadFileVideo();
141+
// 检查本地是否有文件,如果没有则下载
142+
CheckAndPlayVideo();
82143
}
83144

84145
private void OnDestroy()

0 commit comments

Comments
 (0)