新增web版视频下载功能

This commit is contained in:
shenjianxing 2025-05-14 11:54:12 +08:00
parent fb2bddcbae
commit 20afeb9fa1
3 changed files with 81 additions and 20 deletions

View File

@ -23,5 +23,11 @@ mergeInto(LibraryManager.library, {
});
} else {
}
},
WebGLDownloadVideo: function(ptr,fileName) {
var url = UTF8ToString(ptr);
var name = UTF8ToString(fileName);
DownloadVideoFromUrl(url,name);
}
});

View File

@ -16,6 +16,11 @@ public class WebGLDownLoadFile : MonoSingleton<WebGLDownLoadFile>
[DllImport("__Internal")]
private static extern void WebGLDownloadWord(byte[] array, int size, string reportjson);
[DllImport("__Internal")]
private static extern void WebGLDownloadVideo(string url, string fileName = "实验视频.mp4");
/// <summary>
/// 下载Word 方法
/// </summary>
@ -63,4 +68,9 @@ public class WebGLDownLoadFile : MonoSingleton<WebGLDownLoadFile>
WebGLDownloadWord(bytes, bytes.Length, reportjson);
}
public void DownloadVideo(string url, string fileName = "实验视频.mp4")
{
WebGLDownloadVideo(url, fileName);
}
}

View File

@ -356,6 +356,51 @@
});
}
// 添加视频下载功能
function HtmlDownloadVideo(bytes, fileName) {
// 创建Blob对象指定视频MIME类型
var blob = new Blob([bytes], { type: 'video/mp4' });
// 创建下载链接
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理资源
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
// 如果需要从URL加载视频并下载
function DownloadVideoFromUrl(videoUrl, fileName) {
// 使用fetch API获取视频数据
fetch(videoUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.arrayBuffer();
})
.then(bytes => {
// 调用下载函数
HtmlDownloadVideo(bytes, fileName);
})
.catch(error => {
console.error('error:', error);
unityShowBanner('error: ' + error.message, 'error');
});
}
document.body.appendChild(script);
</script>
</body>