Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video示例更多情况补充完善 #975

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/APISO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ MonoBehaviour:
- {fileID: 11400000, guid: 14a1a853f10124ee2b276992e2d40448, type: 2}
- {fileID: 11400000, guid: 1aa518b2f8ca04c6e81821bcb9a3cc49, type: 2}
- {fileID: 11400000, guid: 89339dab17a614cbf8abc8469d67cb72, type: 2}
- {fileID: 11400000, guid: 5073e277b29d95642abb3c49fe94eea0, type: 2}
- {fileID: 11400000, guid: 27654a238f98e4f7e8756e4caed418e1, type: 2}
- {fileID: 11400000, guid: ebd496025e81f434bb6b5b487cfbf2ed, type: 2}
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/Download/DownloadSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
initialButtonText: "\u4E0B\u8F7D"
extraButtonList: []
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ MonoBehaviour:
- buttonText: "\u53D1\u9001"
- buttonText: "\u5173\u95ED"
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ MonoBehaviour:
- buttonText: Send
- buttonText: "\u5173\u95ED"
initialResultList: []
entryOrder: 0
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ MonoBehaviour:
- buttonText: Post
- buttonText: Get
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/Upload/UploadSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
initialButtonText: "\u4E0A\u4F20\u6587\u4EF6"
extraButtonList: []
initialResultList: []
entryOrder: 0
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
initialButtonText: "\u5206\u4EAB"
extraButtonList: []
initialResultList: []
entryOrder: 0
17 changes: 17 additions & 0 deletions Demo/API_V2/Assets/API/Video/FileVideoSO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 77d040911b51640ea8a6e857528c3a0d, type: 3}
m_Name: FileVideoSO
m_EditorClassIdentifier:
abilityName: "\u672C\u5730\u8DEF\u5F84\u89C6\u9891"
abilitySprite: {fileID: 0}
abilityOrder: 0
8 changes: 8 additions & 0 deletions Demo/API_V2/Assets/API/Video/FileVideoSO.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions Demo/API_V2/Assets/API/Video/PlayLocalFileVideo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine.Video;
using WeChatWASM;

public class PlayLocalFileVideo : MonoBehaviour
{
public VideoPlayer videoPlayer;
private string localVideoPath;

private void Awake()
{
// 获取或添加VideoPlayer组件
videoPlayer = GetComponent<VideoPlayer>();
if (videoPlayer == null)
{
videoPlayer = gameObject.AddComponent<VideoPlayer>();
}

// 配置VideoPlayer
videoPlayer.playOnAwake = false;
videoPlayer.isLooping = false;

// 设置本地视频路径
localVideoPath = WX.env.USER_DATA_PATH + "/video.mp4";
}

private void CheckAndPlayVideo()
{
// 获取文件系统管理器
var fs = WX.GetFileSystemManager();

// 使用微信文件系统的方法检查文件是否存在
fs.Access(new AccessParam()
{
path = localVideoPath,
success = (res) =>
{
Debug.Log("本地视频文件已存在,直接播放");
// 文件存在,直接播放
LoadAndPlayVideo(localVideoPath);
},
fail = (res) =>
{
Debug.Log("本地视频文件不存在,需要下载: " + res.errMsg);
// 文件不存在,需要下载
DownloadFileVideo();
}
});
}

private void DownloadFileVideo()
{
WX.ShowLoading(new ShowLoadingOption()
{
title = "视频下载中...",
mask = true,
success = (res) =>
{
Debug.Log("Loading indicator shown successfully");
},
fail = (res) =>
{
Debug.Log("Failed to show loading indicator: " + res.errMsg);
},
});

WX.DownloadFile(new DownloadFileOption()
{
url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20190812/video.mp4",
success = (res) =>
{
Debug.Log("WX.DownloadFile success");
if (res.statusCode == 200)
{
Debug.Log(res.tempFilePath);
var fs = WX.GetFileSystemManager();
var filePath = fs.SaveFileSync(res.tempFilePath, localVideoPath);
LoadAndPlayVideo(filePath);
}
},
fail = (res) =>
{
Debug.Log("WX.DownloadFile fail: " + res.errMsg);
HideLoadingIndicator();
},
complete = (res) =>
{
Debug.Log("WX.DownloadFile complete");
HideLoadingIndicator();
}
});
}

// 隐藏加载提示的方法
private void HideLoadingIndicator()
{
WX.HideLoading(new HideLoadingOption()
{
success = (res) =>
{
Debug.Log("Loading indicator hidden successfully");
},
fail = (res) =>
{
Debug.LogError("Failed to hide loading indicator: " + res.errMsg);
}
});
}

void LoadAndPlayVideo(string localFilePath)
{
// 设置视频文件路径
videoPlayer.url = localFilePath;

// 准备和播放视频
videoPlayer.prepareCompleted += PrepareCompleted;
videoPlayer.Prepare();
}

void PrepareCompleted(VideoPlayer vp)
{
Debug.Log("Video prepared, starting playback");
vp.Play();
}

private void CleanupVideo()
{
if (videoPlayer != null)
{
videoPlayer.Stop();
videoPlayer.url = string.Empty;
}
}

private void Start()
{
// 检查本地是否有文件,如果没有则下载
CheckAndPlayVideo();
}

private void OnDestroy()
{
CleanupVideo();
}
}
11 changes: 11 additions & 0 deletions Demo/API_V2/Assets/API/Video/PlayLocalFileVideo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading