Example #1
0
    private IEnumerator VideoHostingParser(bool isPrepare)
    {
        var videoInfos = _videoHostingParser.GetCachedVideoInfos(_path);

        if (videoInfos == null)
        {
#if UNITY_EDITOR
            _lastEventMsg = "Parsing";
#endif
            _isParsing = true;
            _videoHostingParser.ParseVideoInfos(_path, (res) =>
            {
                _isParsing = false;
            });

            while (_isParsing)
            {
                yield return(null);
            }

            videoInfos = _videoHostingParser.GetCachedVideoInfos(_path);
        }

        var videoInfo = _videoHostingParser.GetBestCompatibleVideo(videoInfos);
        if (videoInfo.RequiresDecryption && !videoInfo.IsDecrypted)
        {
            _videoHostingParser.DecryptVideoUrl(videoInfo, (res) =>
            {
                _mediaPlayer.SetDataSource(new Uri(videoInfo.DownloadUrl));

                if (isPrepare)
                {
                    _mediaPlayer.Prepare();
                }
                else
                {
                    _mediaPlayer.Play();
                }
            });
        }
        else
        {
            _mediaPlayer.SetDataSource(new Uri(videoInfo.DownloadUrl));

            if (isPrepare)
            {
                _mediaPlayer.Prepare();
            }
            else
            {
                _mediaPlayer.Play();
            }
        }
    }
    private IEnumerator VideoPathPreparing(string path, bool playImmediately, IPathPreparedListener listener)
    {
        if (_cachedVideoPaths.ContainsKey(path))
        {
            listener.OnPathPrepared(_cachedVideoPaths[path], playImmediately);
            yield break;
        }

#if UNITY_EDITOR
        _lastEventMsg = "Path Preparing";
#endif

        if (UMPSettings.SupportedPlatform == UMPSettings.Platforms.Android)
        {
            /// Check if we try to play exported videos and wait when export process will be completed
            var exptPaths = UMPSettings.GetSettings().AndroidExportedPaths;
            var filePath  = path.Replace("file:///", "");

            foreach (var exptPath in exptPaths)
            {
                if (exptPath.Contains(filePath))
                {
                    while (!_isExportCompleted)
                    {
                        yield return(null);
                    }

                    if (_cachedVideoPaths.ContainsKey(filePath))
                    {
                        listener.OnPathPrepared(_cachedVideoPaths[filePath], playImmediately);
                        yield break;
                    }

                    break;
                }
            }

            if ((_mediaPlayer.Options as PlayerOptionsAndroid).PlayerType ==
                PlayerOptionsAndroid.PlayerTypes.LibVLC &&
                MediaPlayerHelper.IsAssetsFile(path))
            {
                var tempFilePath = System.IO.Path.Combine(Application.temporaryCachePath, filePath);
                if (File.Exists(tempFilePath))
                {
                    _cachedVideoPaths.Add(path, tempFilePath);
                    listener.OnPathPrepared(tempFilePath, playImmediately);
                    yield break;
                }

                var www = new WWW(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
                yield return(www);

                if (string.IsNullOrEmpty(www.error))
                {
                    var tempFile = new FileInfo(tempFilePath);
                    tempFile.Directory.Create();
                    File.WriteAllBytes(tempFile.FullName, www.bytes);
                    _cachedVideoPaths.Add(path, tempFilePath);
                    path = tempFilePath;
                }
                else
                {
                    Debug.LogError("Can't create temp file from asset folder: " + www.error);
                }
                www.Dispose();
            }
        }

        if (_videoHostingParser.IsVideoHostingUrl(path))
        {
            var videoInfos = _videoHostingParser.GetCachedVideoInfos(path);

            if (videoInfos == null)
            {
                var isError = false;
                _isParsing = true;

                _videoHostingParser.ParseVideoInfos(path, (res) =>
                {
                    _isParsing = false;
                }, (error) =>
                {
                    isError = true;
                    Debug.LogError(error);
                });

                while (_isParsing)
                {
                    if (isError)
                    {
                        _isParsing = false;
                        yield break;
                    }

                    yield return(null);
                }

                videoInfos = _videoHostingParser.GetCachedVideoInfos(path);
            }

            var videoInfo  = _videoHostingParser.GetBestCompatibleVideo(videoInfos);
            var directLink = videoInfo.DownloadUrl;

            if (videoInfo.RequiresDecryption && !videoInfo.IsDecrypted)
            {
                var isDecrypted = false;
                _videoHostingParser.DecryptVideoUrl(videoInfo, (res) =>
                {
                    directLink  = videoInfo.DownloadUrl;
                    isDecrypted = true;
                });

                while (!isDecrypted)
                {
                    yield return(null);
                }
            }
            _cachedVideoPaths.Add(path, directLink);
            path = directLink;
        }

        listener.OnPathPrepared(path, playImmediately);
        yield return(null);
    }
    private IEnumerator VideoPathPreparing(string path, bool playImmediately, IPathPreparedListener listener)
    {
        if (_cachedVideoPaths.ContainsKey(path))
        {
            listener.OnPathPrepared(_cachedVideoPaths[path], playImmediately);
            yield break;
        }

#if UNITY_EDITOR
        _lastEventMsg = "Path Preparing";
#endif

        if (UMPSettings.SupportedPlatform == UMPSettings.Platforms.Android &&
            !UMPSettings.Instance.UseAndroidNative &&
            MediaPlayerHelper.IsAssetsFile(path))
        {
            var filePath     = path.Replace("file:///", "");
            var tempFilePath = System.IO.Path.Combine(Application.temporaryCachePath, filePath);
            if (File.Exists(tempFilePath))
            {
                _cachedVideoPaths.Add(path, tempFilePath);
                listener.OnPathPrepared(tempFilePath, playImmediately);
                yield break;
            }

            var www = new WWW(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
            yield return(www);

            if (string.IsNullOrEmpty(www.error))
            {
                File.WriteAllBytes(tempFilePath, www.bytes);
                _cachedVideoPaths.Add(path, tempFilePath);
                path = tempFilePath;
            }
            else
            {
                Debug.LogError("Can't create temp file from asset folder: " + www.error);
            }
            www.Dispose();
        }

        if (_videoHostingParser.IsVideoHostingUrl(path))
        {
            var videoInfos = _videoHostingParser.GetCachedVideoInfos(path);

            if (videoInfos == null)
            {
                var isError = false;
                _isParsing = true;

                _videoHostingParser.ParseVideoInfos(path, (res) =>
                {
                    _isParsing = false;
                }, (error) =>
                {
                    isError = true;
                    Debug.LogError(error);
                });

                while (_isParsing)
                {
                    if (isError)
                    {
                        _isParsing = false;
                        yield break;
                    }

                    yield return(null);
                }

                videoInfos = _videoHostingParser.GetCachedVideoInfos(path);
            }

            var videoInfo  = _videoHostingParser.GetBestCompatibleVideo(videoInfos);
            var directLink = videoInfo.DownloadUrl;

            if (videoInfo.RequiresDecryption && !videoInfo.IsDecrypted)
            {
                var isDecrypted = false;
                _videoHostingParser.DecryptVideoUrl(videoInfo, (res) =>
                {
                    directLink  = videoInfo.DownloadUrl;
                    isDecrypted = true;
                });

                while (!isDecrypted)
                {
                    yield return(null);
                }
            }
            _cachedVideoPaths.Add(path, directLink);
            path = directLink;
        }

        listener.OnPathPrepared(path, playImmediately);
        yield return(null);
    }