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
+
13
+ private void Awake ( )
14
+ {
15
+ // 获取或添加VideoPlayer组件
16
+ videoPlayer = GetComponent < VideoPlayer > ( ) ;
17
+ if ( videoPlayer == null )
18
+ {
19
+ videoPlayer = gameObject . AddComponent < VideoPlayer > ( ) ;
20
+ }
21
+
22
+ // 配置VideoPlayer
23
+ videoPlayer . playOnAwake = false ;
24
+ videoPlayer . isLooping = false ;
25
+ }
26
+
27
+ private void DownloadFileVideo ( )
28
+ {
29
+ WX . DownloadFile ( new DownloadFileOption ( )
30
+ {
31
+ url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20190812/video.mp4" ,
32
+ success = ( res ) =>
33
+ {
34
+ Debug . Log ( "WX.DownloadFile success" ) ;
35
+ if ( res . statusCode == 200 )
36
+ {
37
+ Debug . Log ( res . tempFilePath ) ;
38
+ var fs = WX . GetFileSystemManager ( ) ;
39
+ var filePath = fs . SaveFileSync ( res . tempFilePath , WX . env . USER_DATA_PATH + "/video.mp4" ) ;
40
+ LoadAndPlayVideo ( filePath ) ;
41
+ }
42
+ } ,
43
+ fail = ( res ) =>
44
+ {
45
+ Debug . Log ( "WX.DownloadFile fail" ) ;
46
+ } ,
47
+ complete = ( res ) =>
48
+ {
49
+ Debug . Log ( "WX.DownloadFile complete" ) ;
50
+ }
51
+ } ) ;
52
+ }
53
+
54
+ void LoadAndPlayVideo ( string localFilePath )
55
+ {
56
+ // 设置视频文件路径
57
+ videoPlayer . url = localFilePath ;
58
+
59
+ // 准备和播放视频
60
+ videoPlayer . prepareCompleted += PrepareCompleted ;
61
+ videoPlayer . Prepare ( ) ;
62
+ }
63
+
64
+ void PrepareCompleted ( VideoPlayer vp )
65
+ {
66
+ Debug . Log ( "Video prepared, starting playback" ) ;
67
+ vp . Play ( ) ;
68
+ }
69
+
70
+ private void CleanupVideo ( )
71
+ {
72
+ if ( videoPlayer != null )
73
+ {
74
+ videoPlayer . Stop ( ) ;
75
+ videoPlayer . url = string . Empty ;
76
+ }
77
+ }
78
+
79
+ private void Start ( )
80
+ {
81
+ DownloadFileVideo ( ) ;
82
+ }
83
+
84
+ private void OnDestroy ( )
85
+ {
86
+ CleanupVideo ( ) ;
87
+ }
88
+ }
0 commit comments