{"id":4902,"date":"2023-06-13T19:45:20","date_gmt":"2023-06-13T11:45:20","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4902"},"modified":"2023-07-19T18:04:11","modified_gmt":"2023-07-19T10:04:11","slug":"videoplayerdebug","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4902","title":{"rendered":"UnityVideoPlayer\u8fdc\u7a0b\u4e0b\u8f7d\u8c03\u8bd5"},"content":{"rendered":"<p>\u53ef\u4ee5\u53cd\u590d\u4e0b\u8f7d\u6307\u5b9a\u7684\u89c6\u9891\u5e76\u64ad\u653e\uff0c\u6d4b\u8bd5\u89c6\u9891\u64ad\u653e\u7684\u517c\u5bb9\u6027<\/p>\n<pre><code class=\"language-c#\">using UnityEngine;\nusing System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine.Networking;\n\n[RequireComponent(typeof(UnityEngine.Video.VideoPlayer))]\n[RequireComponent(typeof(UnityEngine.UI.RawImage))]\npublic class MyVideoPlayer : MonoBehaviour\n{\n    private RenderTexture mRenderTexture;\n\n#if UNITY_ANDROID\n    const float GUIScale = 3;\n#else\n    const float GUIScale = 1;\n#endif\n\n    private GUILayoutOption[] InputBoxStyle  = new GUILayoutOption[] { GUILayout.MaxWidth(500 * GUIScale), GUILayout.MaxHeight(40* GUIScale) };\n    private GUILayoutOption[] SliderBoxStyle = new GUILayoutOption[] { GUILayout.MaxWidth(500 * GUIScale), GUILayout.MaxHeight(40* GUIScale) };\n    private GUILayoutOption[] ButtonStyle    = new GUILayoutOption[] { GUILayout.MaxWidth(200 * GUIScale), GUILayout.MaxHeight(40* GUIScale) };\n\n    private string mServerUrl = &quot;http:\/\/192.168.3.23\/&quot;;\n    private int mFilename = 1;\n\n    private IEnumerator mDownloadTask;\n    private string mProgressLabel = string.Empty;\n    private Dictionary&lt;string, string&gt; mDownloadFiles = new Dictionary&lt;string, string&gt;();\n    private GUISkin mGUISkin;\n    void OnGUI()\n    {\n        GUILayout.BeginVertical();\n\n        mServerUrl = GUILayout.TextArea(mServerUrl, InputBoxStyle);\n\n        mFilename = (int)GUILayout.HorizontalSlider(mFilename, 1, 20, SliderBoxStyle);\n\n        var mPlayerUrl = mServerUrl + mFilename.ToString() + &quot;.mp4&quot;;\n        GUILayout.Label(mPlayerUrl);\n\n        GUILayout.BeginHorizontal();\n\n        if (GUILayout.Button(&quot;\u4e0b\u8f7d&quot;, ButtonStyle))\n        {\n            if (mDownloadFiles.TryGetValue(mPlayerUrl, out var localFilename))\n            {\n                ShowTips($&quot;{mPlayerUrl} \u6587\u4ef6\u5df2\u4e0b\u8f7d {localFilename}&quot;);\n            }\n            else\n            {\n                if (mDownloadTask != null)\n                {\n                    StopCoroutine(mDownloadTask);\n                    mDownloadTask = null;\n                }\n                mDownloadTask = DownloadWebFile(mPlayerUrl);\n                StartCoroutine(mDownloadTask);\n            }\n        }\n        if (GUILayout.Button(&quot;\u64ad\u653e&quot;, ButtonStyle))\n        {\n            if (mDownloadFiles.TryGetValue(mPlayerUrl, out var localFilename))\n            {\n                PlayByUrl(localFilename);\n            }\n            else\n            {\n                ShowTips($&quot;{mPlayerUrl} \u6587\u4ef6\u672a\u4e0b\u8f7d&quot;);\n            }\n        }\n        GUILayout.EndHorizontal();\n\n        if (mProgressLabel.Length &gt; 0)\n        {\n            GUILayout.Label(mProgressLabel);\n        }\n\n        GUILayout.EndVertical();\n    }\n\n    void ShowTips(string tips)\n    {\n        mProgressLabel = tips;\n    }\n\n    IEnumerator DownloadWebFile(string remoteUrl)\n    {\n        UnityWebRequest unityWebRequest = UnityWebRequest.Get(remoteUrl);\n\n        Debug.Log($&quot;DownloadStart: {remoteUrl}&quot;);\n        ShowTips($&quot;DownloadStart: {remoteUrl} &quot;);\n\n        unityWebRequest.SendWebRequest();\n\n        for(int i=0; i&lt;1024*1024; ++i)\n        {\n            ShowTips($&quot;DownloadProgress: {unityWebRequest.downloadProgress} {remoteUrl} &quot;);\n            yield return null;\n\n            if (unityWebRequest.isDone &amp;&amp; unityWebRequest.result == UnityEngine.Networking.UnityWebRequest.Result.Success)\n            {\n                break;\n            }\n        }\n\n        Debug.Log($&quot;DownloadFinish: {remoteUrl}&quot;);\n        ShowTips($&quot;DownloadFinish: {remoteUrl} &quot;);\n\n        var downloadHandler=unityWebRequest.downloadHandler;\n\n        var localFilename = System.IO.Path.Combine(Application.persistentDataPath, $&quot;{DateTime.Now.Ticks}.mp4&quot;).Replace(&quot;\\\\&quot;, &quot;\/&quot;);\n        System.IO.File.WriteAllBytes(localFilename, downloadHandler.data);\n\n        mDownloadFiles[remoteUrl] = localFilename;\n    }\n\n    void PlayByUrl(string localFilename)\n    {\n        var videoPlayer = this.GetComponent&lt;UnityEngine.Video.VideoPlayer&gt;();\n        if (videoPlayer != null)\n        {\n            videoPlayer.Stop();\n\n            videoPlayer.source = UnityEngine.Video.VideoSource.Url;\n            videoPlayer.url = localFilename;\n            videoPlayer.isLooping = true;\n\n            Debug.Log($&quot;Play:{localFilename}&quot;);\n            ShowTips($&quot;Play:{localFilename} &quot;);\n\n            videoPlayer.Play();\n        }\n    }\n\n    void Start()\n    {\n        var videoPlayer = this.GetComponent&lt;UnityEngine.Video.VideoPlayer&gt;();\n        var rawImage = this.GetComponent&lt;UnityEngine.UI.RawImage&gt;();\n        if (rawImage != null &amp;&amp; videoPlayer != null)\n        {\n            videoPlayer.errorReceived += OnError;\n\n            var videoWidth = 1136;\n            var videoHeight = 640;\n\n            if (mRenderTexture == null)\n            {\n                mRenderTexture = RenderTexture.GetTemporary(videoWidth, videoHeight);\n            }\n\n            if (mRenderTexture != null)\n            {\n                videoPlayer.targetTexture = mRenderTexture;\n                rawImage.texture = mRenderTexture;\n            }\n        }\n    }\n\n    void OnError(UnityEngine.Video.VideoPlayer source, string msg)\n    {\n        Debug.LogError($&quot;[ERROR] {source.name} {msg}&quot;);\n        source.Stop();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u53cd\u590d\u4e0b\u8f7d\u6307\u5b9a\u7684\u89c6\u9891\u5e76\u64ad\u653e\uff0c\u6d4b\u8bd5\u89c6\u9891\u64ad\u653e\u7684\u517c\u5bb9\u6027 using UnityEngine; using Syst [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4902"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4902"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4902\/revisions"}],"predecessor-version":[{"id":4973,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4902\/revisions\/4973"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}