Skip to content

Commit af9960e

Browse files
authoredMar 20, 2025··
Merge pull request #975 from VincentWSZ/File_Video
Video示例补充本地文件播放
2 parents 00bc8b2 + 7e4b610 commit af9960e

17 files changed

+3345
-0
lines changed
 

‎Demo/API_V2/Assets/API/APISO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ MonoBehaviour:
3636
- {fileID: 11400000, guid: 14a1a853f10124ee2b276992e2d40448, type: 2}
3737
- {fileID: 11400000, guid: 1aa518b2f8ca04c6e81821bcb9a3cc49, type: 2}
3838
- {fileID: 11400000, guid: 89339dab17a614cbf8abc8469d67cb72, type: 2}
39+
- {fileID: 11400000, guid: 5073e277b29d95642abb3c49fe94eea0, type: 2}
3940
- {fileID: 11400000, guid: 27654a238f98e4f7e8756e4caed418e1, type: 2}
4041
- {fileID: 11400000, guid: ebd496025e81f434bb6b5b487cfbf2ed, type: 2}

‎Demo/API_V2/Assets/API/Network/Download/DownloadSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ MonoBehaviour:
2020
initialButtonText: "\u4E0B\u8F7D"
2121
extraButtonList: []
2222
initialResultList: []
23+
entryOrder: 0

‎Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ MonoBehaviour:
2727
- buttonText: "\u53D1\u9001"
2828
- buttonText: "\u5173\u95ED"
2929
initialResultList: []
30+
entryOrder: 0

‎Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ MonoBehaviour:
3030
- buttonText: Send
3131
- buttonText: "\u5173\u95ED"
3232
initialResultList: []
33+
entryOrder: 0

‎Demo/API_V2/Assets/API/Network/UnityWebRequest/WebRequest SO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ MonoBehaviour:
2222
- buttonText: Post
2323
- buttonText: Get
2424
initialResultList: []
25+
entryOrder: 0

‎Demo/API_V2/Assets/API/Network/Upload/UploadSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ MonoBehaviour:
2020
initialButtonText: "\u4E0A\u4F20\u6587\u4EF6"
2121
extraButtonList: []
2222
initialResultList: []
23+
entryOrder: 0

‎Demo/API_V2/Assets/API/Share/ShareAppMessage/ShareAppMessageSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ MonoBehaviour:
2020
initialButtonText: "\u5206\u4EAB"
2121
extraButtonList: []
2222
initialResultList: []
23+
entryOrder: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 77d040911b51640ea8a6e857528c3a0d, type: 3}
13+
m_Name: FileVideoSO
14+
m_EditorClassIdentifier:
15+
abilityName: "\u672C\u5730\u8DEF\u5F84\u89C6\u9891"
16+
abilitySprite: {fileID: 0}
17+
abilityOrder: 0

‎Demo/API_V2/Assets/API/Video/FileVideoSO.asset.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}

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

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Demo/API_V2/Assets/Scenes/FileVideoScene.unity

+2,619
Large diffs are not rendered by default.

‎Demo/API_V2/Assets/Scenes/FileVideoScene.unity.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!850595691 &4890085278179872738
4+
LightingSettings:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_Name: FileVideoSceneSettings
10+
serializedVersion: 4
11+
m_GIWorkflowMode: 1
12+
m_EnableBakedLightmaps: 1
13+
m_EnableRealtimeLightmaps: 0
14+
m_RealtimeEnvironmentLighting: 1
15+
m_BounceScale: 1
16+
m_AlbedoBoost: 1
17+
m_IndirectOutputScale: 1
18+
m_UsingShadowmask: 1
19+
m_BakeBackend: 1
20+
m_LightmapMaxSize: 1024
21+
m_BakeResolution: 40
22+
m_Padding: 2
23+
m_LightmapCompression: 3
24+
m_AO: 0
25+
m_AOMaxDistance: 1
26+
m_CompAOExponent: 1
27+
m_CompAOExponentDirect: 0
28+
m_ExtractAO: 0
29+
m_MixedBakeMode: 2
30+
m_LightmapsBakeMode: 1
31+
m_FilterMode: 1
32+
m_LightmapParameters: {fileID: 15204, guid: 0000000000000000f000000000000000, type: 0}
33+
m_ExportTrainingData: 0
34+
m_TrainingDataDestination: TrainingData
35+
m_RealtimeResolution: 2
36+
m_ForceWhiteAlbedo: 0
37+
m_ForceUpdates: 0
38+
m_FinalGather: 0
39+
m_FinalGatherRayCount: 256
40+
m_FinalGatherFiltering: 1
41+
m_PVRCulling: 1
42+
m_PVRSampling: 1
43+
m_PVRDirectSampleCount: 32
44+
m_PVRSampleCount: 512
45+
m_PVREnvironmentSampleCount: 512
46+
m_PVREnvironmentReferencePointCount: 2048
47+
m_LightProbeSampleCountMultiplier: 4
48+
m_PVRBounces: 2
49+
m_PVRMinBounces: 2
50+
m_PVREnvironmentMIS: 0
51+
m_PVRFilteringMode: 2
52+
m_PVRDenoiserTypeDirect: 0
53+
m_PVRDenoiserTypeIndirect: 0
54+
m_PVRDenoiserTypeAO: 0
55+
m_PVRFilterTypeDirect: 0
56+
m_PVRFilterTypeIndirect: 0
57+
m_PVRFilterTypeAO: 0
58+
m_PVRFilteringGaussRadiusDirect: 1
59+
m_PVRFilteringGaussRadiusIndirect: 5
60+
m_PVRFilteringGaussRadiusAO: 2
61+
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
62+
m_PVRFilteringAtrousPositionSigmaIndirect: 2
63+
m_PVRFilteringAtrousPositionSigmaAO: 1
64+
m_PVRTiledBaking: 0

‎Demo/API_V2/Assets/Scenes/FileVideoSceneSettings.lighting.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Demo/API_V2/Assets/Scenes/MainScene.unity

+452
Large diffs are not rendered by default.

‎Demo/API_V2/ProjectSettings/EditorBuildSettings.asset

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ EditorBuildSettings:
2626
- enabled: 1
2727
path: Assets/Scenes/WXVideoScene.unity
2828
guid: be998cba51d48449d8d0cdd7186b3e1a
29+
- enabled: 1
30+
path: Assets/Scenes/FileVideoScene.unity
31+
guid: fab74273392d6a147b84e266de8196bd
2932
m_configObjects:
3033
com.unity.renderstreaming.settings: {fileID: 11400000, guid: eaaad242393318e4f85c45e69c8837f0,
3134
type: 2}

0 commit comments

Comments
 (0)
Please sign in to comment.