9
9
public class PlayLocalFileVideo : MonoBehaviour
10
10
{
11
11
public VideoPlayer videoPlayer ;
12
+ private string localVideoPath ;
12
13
13
14
private void Awake ( )
14
15
{
@@ -22,10 +23,51 @@ private void Awake()
22
23
// 配置VideoPlayer
23
24
videoPlayer . playOnAwake = false ;
24
25
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
+ } ) ;
25
53
}
26
54
27
55
private void DownloadFileVideo ( )
28
56
{
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
+
29
71
WX . DownloadFile ( new DownloadFileOption ( )
30
72
{
31
73
url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20190812/video.mp4" ,
@@ -36,17 +78,35 @@ private void DownloadFileVideo()
36
78
{
37
79
Debug . Log ( res . tempFilePath ) ;
38
80
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 ) ;
40
82
LoadAndPlayVideo ( filePath ) ;
41
83
}
42
84
} ,
43
85
fail = ( res ) =>
44
86
{
45
- Debug . Log ( "WX.DownloadFile fail" ) ;
87
+ Debug . Log ( "WX.DownloadFile fail: " + res . errMsg ) ;
88
+ HideLoadingIndicator ( ) ;
46
89
} ,
47
90
complete = ( res ) =>
48
91
{
49
92
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 ) ;
50
110
}
51
111
} ) ;
52
112
}
@@ -78,7 +138,8 @@ private void CleanupVideo()
78
138
79
139
private void Start ( )
80
140
{
81
- DownloadFileVideo ( ) ;
141
+ // 检查本地是否有文件,如果没有则下载
142
+ CheckAndPlayVideo ( ) ;
82
143
}
83
144
84
145
private void OnDestroy ( )
0 commit comments