UnityWebRequest下载Mp4视频,VideoPlayer解码失败
123 浏览2026年04月30日作者:zhumeng666
    private IEnumerator WXLoadVideoWithCache1(string url)
    {
        string cachePath = TT.GetFileSystemManager().GetLocalCachedPathForUrl(url);
        if (TT.GetFileSystemManager().IsUrlCached(url))
        {
            Debug.Log($"[VideoTest] 缓存命中: {cachePath}");
            PlayLocalVideo(cachePath);
            yield break;
        }
        Debug.Log($"[VideoTest] cachePath = {cachePath}");
        Debug.Log($"[VideoTest] url={url}");

        Debug.Log($"[VideoTest] 缓存未命中,预下载: {url}");
        m_txtLog.text = "预下载中...";

        using (UnityWebRequest req = UnityWebRequest.Get(url))
        {
            yield return req.SendWebRequest();
            while (!req.isDone)
            {
                m_txtLog.text = $"预下载中... {req.downloadProgress * 100:F0}%";
                yield return null;
            }

            m_txtLog.text = $"预下载中... {req.downloadProgress * 100:F0}%";

            if (req.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError($"[VideoTest] 预下载失败: {req.error}");
                m_txtLog.text = $"预下载失败: {req.error}";
                yield break;
            }

            if (TT.GetFileSystemManager().IsUrlCached(url))
            {
                Debug.Log($"[VideoTest] 预下载完成,本地路径: {cachePath}");
                PlayLocalVideo(cachePath);
            }
        }
    }
    private void PlayLocalVideo(string path)
    {
        videoPlayer.url = path;
        m_txtLog.text = $"PrepareTime: {m_prepareTime}s firstFrameTime: {m_firstFrameTime}s";
        videoPlayer.sendFrameReadyEvents = true;
        videoPlayer.Prepare();
    }

报错:
 An error has occurred in the decoding of the media resource, after the resource was established to be usable.
VideoPlayer cannot play url : ttfile://user/__sc_http_cache_files__/eb1a72f450f8b29b86e764bfed7a3b09

Cannot read file.

使用
            byte[] videoBytes = req.downloadHandler.data;

            var _filePath = Path.Combine(TTSDK.TTFileSystemManager.USER_DATA_PATH, "moov_infront.mp4");
            var fd = TT.GetFileSystemManager().OpenSync(new()
            {
                filePath = _filePath,
                flag = "w+"
            });

            TT.GetFileSystemManager().WriteFileSync(fd, videoBytes);
报错:
writeFileSync fail API_ERROR: writeFileSync:fail permission denied, writeFileSync C12CE39B-232A-4BB2-95F3-661E49BB4B68

小米12X测试没有问题,苹果17测试出现上述问题,并且在苹果17中,将视频文件放到StreamingAssets文件中,作为首包资源,视频正常播放
最后一次编辑于 2026 年 04 月 30 日
共 1 条评论

相关问答

专题推荐

热门文章

热门问答