1
+ using System ;
2
+ using System . Collections ;
3
+ using UnityEngine ;
4
+ using UnityEngine . Networking ;
5
+ using UnityEngine . UI ;
6
+ using UnityEngine . Video ;
7
+ using WeChatWASM ;
8
+
9
+ public class PlayLocalFileVideo : MonoBehaviour
10
+ {
11
+ public VideoPlayer videoPlayer ;
12
+ private string localVideoPath ;
13
+
14
+ private void Awake ( )
15
+ {
16
+ // 获取或添加VideoPlayer组件
17
+ videoPlayer = GetComponent < VideoPlayer > ( ) ;
18
+ if ( videoPlayer == null )
19
+ {
20
+ videoPlayer = gameObject . AddComponent < VideoPlayer > ( ) ;
21
+ }
22
+
23
+ // 配置VideoPlayer
24
+ videoPlayer . playOnAwake = false ;
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
+ } ) ;
53
+ }
54
+
55
+ private void DownloadFileVideo ( )
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
+
71
+ WX . DownloadFile ( new DownloadFileOption ( )
72
+ {
73
+ url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20190812/video.mp4" ,
74
+ success = ( res ) =>
75
+ {
76
+ Debug . Log ( "WX.DownloadFile success" ) ;
77
+ if ( res . statusCode == 200 )
78
+ {
79
+ Debug . Log ( res . tempFilePath ) ;
80
+ var fs = WX . GetFileSystemManager ( ) ;
81
+ var filePath = fs . SaveFileSync ( res . tempFilePath , localVideoPath ) ;
82
+ LoadAndPlayVideo ( filePath ) ;
83
+ }
84
+ } ,
85
+ fail = ( res ) =>
86
+ {
87
+ Debug . Log ( "WX.DownloadFile fail: " + res . errMsg ) ;
88
+ HideLoadingIndicator ( ) ;
89
+ } ,
90
+ complete = ( res ) =>
91
+ {
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 ) ;
110
+ }
111
+ } ) ;
112
+ }
113
+
114
+ void LoadAndPlayVideo ( string localFilePath )
115
+ {
116
+ // 设置视频文件路径
117
+ videoPlayer . url = localFilePath ;
118
+
119
+ // 准备和播放视频
120
+ videoPlayer . prepareCompleted += PrepareCompleted ;
121
+ videoPlayer . Prepare ( ) ;
122
+ }
123
+
124
+ void PrepareCompleted ( VideoPlayer vp )
125
+ {
126
+ Debug . Log ( "Video prepared, starting playback" ) ;
127
+ vp . Play ( ) ;
128
+ }
129
+
130
+ private void CleanupVideo ( )
131
+ {
132
+ if ( videoPlayer != null )
133
+ {
134
+ videoPlayer . Stop ( ) ;
135
+ videoPlayer . url = string . Empty ;
136
+ }
137
+ }
138
+
139
+ private void Start ( )
140
+ {
141
+ // 检查本地是否有文件,如果没有则下载
142
+ CheckAndPlayVideo ( ) ;
143
+ }
144
+
145
+ private void OnDestroy ( )
146
+ {
147
+ CleanupVideo ( ) ;
148
+ }
149
+ }
0 commit comments