Ejemplo n.º 1
0
 /// <summary>
 /// Add video file to video file list.
 /// </summary>
 public void SetRootFolder()
 {
     if (Directory.Exists(PathConfig.SaveFolder))
     {
         DirectoryInfo direction = new DirectoryInfo(PathConfig.SaveFolder);
         FileInfo[]    files     = direction.GetFiles("*", SearchOption.AllDirectories);
         videoFiles.Clear();
         for (int i = 0; i < files.Length; i++)
         {
             if (files[i].Name.EndsWith(".mp4"))
             {
                 videoFiles.Add(PathConfig.SaveFolder + files[i].Name);
                 continue;
             }
         }
     }
     // Init VideoPlayer properties.
     videoPlayerImpl                           = gameObject.GetComponent <UnityEngine.Video.VideoPlayer>();
     videoPlayerImpl.source                    = UnityEngine.Video.VideoSource.Url;
     videoPlayerImpl.playOnAwake               = false;
     videoPlayerImpl.renderMode                = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
     videoPlayerImpl.targetCamera              = Camera.main;
     videoPlayerImpl.audioOutputMode           = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
     videoPlayerImpl.controlledAudioTrackCount = 1;
     videoPlayerImpl.aspectRatio               = UnityEngine.Video.VideoAspectRatio.Stretch;
     if (gameObject.GetComponent <AudioSource>() != null)
     {
         videoPlayerImpl.SetTargetAudioSource(0, gameObject.GetComponent <AudioSource>());
         gameObject.GetComponent <AudioSource>().clip = null;
     }
 }
Ejemplo n.º 2
0
        public VideoHandler(RawImage videoHolder, PromoData promoData, System.Action endCallback, RenderTexture renderTexture = null)
        {
            mVideoHolder   = videoHolder;
            mPromoData     = promoData;
            mRenderTexture = renderTexture;
            mEndCallback   = endCallback;

            mVideoPlayer = mVideoHolder.gameObject.GetComponent <UnityEngine.Video.VideoPlayer>();
            if (mVideoPlayer == null)
            {
                mVideoPlayer = mVideoHolder.gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
            }
            mVideoPlayer.loopPointReached += OnLoopPointReached;

            mAudioSource = mVideoHolder.gameObject.GetComponent <AudioSource>();
            if (mAudioSource == null)
            {
                mAudioSource = mVideoHolder.gameObject.AddComponent <AudioSource>();
            }

            mVideoPlayer.source          = UnityEngine.Video.VideoSource.Url;
            mVideoPlayer.url             = mPromoData.VideoFileDownloaded;
            mVideoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
            mVideoPlayer.EnableAudioTrack(0, true);
            mVideoPlayer.SetTargetAudioSource(0, mAudioSource);
            mVideoPlayer.Prepare();

            if (mRenderTexture == null)
            {
                mRenderTexture = new RenderTexture(int.Parse(mPromoData.width), int.Parse(mPromoData.height), 16, RenderTextureFormat.ARGB32);
            }
            mVideoPlayer.targetTexture = mRenderTexture;
            mVideoHolder.texture       = mRenderTexture;
        }
        public override void CreatePlayer()
        {
            _mediaPlayer = gameObject.GetComponent <UnityEngine.Video.VideoPlayer>();

            if (_mediaPlayer == null)
            {
                _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
            }

            _audioSource = gameObject.GetComponent <AudioSource>();

            if (_audioSource == null)
            {
                _audioSource = gameObject.AddComponent <AudioSource>();
            }

            _mediaPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
            _mediaPlayer.SetTargetAudioSource(0, _audioSource);
            _mediaPlayer.renderMode        = UnityEngine.Video.VideoRenderMode.APIOnly;
            _mediaPlayer.prepareCompleted += OnVideoLoadingComplete;
            _mediaPlayer.EnableAudioTrack(0, true);
        }
Ejemplo n.º 4
0
        /// <param name="clip">VideoClip reference</param>
        public override bool SetValues(PlayerValues values)
        {
            _mediaPlayer = gameObject.GetComponent <UnityEngine.Video.VideoPlayer>();

            if (_mediaPlayer == null)
            {
                // no media component already added to this component, try adding a MediaPlayer component
                try
                {
                    _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
                }
                catch (Exception e)
                {
                    Debug.LogError("AVProVideo not found in project: " + e.ToString());
                    return(false);
                }
            }

            _audioSource = gameObject.GetComponent <AudioSource>();

            if (_audioSource == null)
            {
                _audioSource = gameObject.AddComponent <AudioSource>();
            }
            if (_mediaPlayer == null)
            {
                _mediaPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
            }

            _mediaPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
            _mediaPlayer.SetTargetAudioSource(0, _audioSource);
            _mediaPlayer.renderMode        = UnityEngine.Video.VideoRenderMode.APIOnly;
            _mediaPlayer.clip              = values.videoClip;
            _mediaPlayer.prepareCompleted += OnVideoLoadingComplete;
            _mediaPlayer.EnableAudioTrack(0, true);
            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 繪製轉場圖片
        /// </summary>
        /// <param name="_step">繪製完後要進入的腳本位置</param>
        /// <param name="sceneName">Scene名稱</param>
        private static void DrawSlidePic(int _step)
        {
            GameObject parent;

            switch (KK_FBIOpenUp.nowGameMode)
            {
            case KK_FBIOpenUp.GameMode.Studio:
                parent = GameObject.Find("StudioScene/Canvas Main Menu");
                break;

            case KK_FBIOpenUp.GameMode.MainGame:
                parent = GameObject.Find("ActionScene/UI/ActionMenuCanvas/ModeAnimation");
                break;

            case KK_FBIOpenUp.GameMode.Maker:
                parent = GameObject.Find("CustomScene/CustomRoot/FrontUIGroup/CustomUIGroup/CvsCoordinateType/redBagBtn");
                break;

            //FreeH死都不成功,放棄
            //case KK_FBIOpenUp.GameMode.FreeH:
            //    parent = GameObject.Find("CommonSpace");
            //    break;
            default:
                parent = GameObject.FindObjectsOfType <GameObject>()[0];    //Not tested
                break;
            }
            GameObject gameObject = new GameObject();

            gameObject.transform.SetParent(parent.transform, false);
            gameObject.SetActive(false);
            if (null != shiftPicture)
            {
                GameObject.Destroy(shiftPicture.Transform.parent.gameObject);
                shiftPicture.image = null;
                shiftPicture.video = null;
                shiftPicture       = null;
            }
            shiftPicture = new ShiftPicture();

            //如果影片不存在,用熊吉代替
            bool noVideoFallback = _step == 20 && null == KK_FBIOpenUp.videoPath;

            if (noVideoFallback)
            {
                _step = 2;
            }

            switch (_step)
            {
            case 1:
                //小學生真是太棒了
                shiftPicture.type  = ShiftPicture.Type.picture;
                shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.saikodaze.jpg", 800, 657));
                shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f * 800 / 657, Screen.height / 1.5f);
                Right2Center();
                break;

            case 2:
                //熊吉逮捕
                shiftPicture.type  = ShiftPicture.Type.picture;
                shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.Kumakichi.jpg", 640, 480));
                shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f * 640 / 480, Screen.height / 1.5f);
                Left2Center();
                break;

            case 10:
                //幼女退光線
                shiftPicture.type  = ShiftPicture.Type.picture;
                shiftPicture.image = UIUtility.CreateImage("", gameObject.transform, Extension.Extension.LoadNewSprite("KK_FBIOpenUp.Resources.beam.png", 700, 700));
                shiftPicture.image.rectTransform.sizeDelta = new Vector2(Screen.height / 1.25f, Screen.height / 1.25f);
                Right2Center();
                break;

            case 20:
                //FBI Open Up影片
                shiftPicture.type = ShiftPicture.Type.video;

                shiftPicture.video = UIUtility.CreateRawImage("", gameObject.transform);
                shiftPicture.video.rectTransform.sizeDelta = new Vector2(Screen.height / 1.5f, Screen.height / 1.5f);

                UnityEngine.Video.VideoPlayer videoPlayer = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
                AudioSource audioSource = gameObject.AddComponent <AudioSource>();
                videoPlayer.playOnAwake = false;
                audioSource.playOnAwake = false;
                videoPlayer.renderMode  = UnityEngine.Video.VideoRenderMode.APIOnly;

                //videoPlayer.url= "../UserData/audio/FBI.mp4";
                videoPlayer.url = KK_FBIOpenUp.videoPath;

                //Set Audio Output to AudioSource
                videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;

                //Assign the Audio from Video to AudioSource to be played
                videoPlayer.EnableAudioTrack(0, true);
                videoPlayer.SetTargetAudioSource(0, audioSource);

                Logger.LogDebug($"{videoPlayer.url}");
                videoPlayer.isLooping = true;

                //先把他移到螢幕外啟用,否則未啟用無法Prepare,而直接啟用會出現白色畫面
                shiftPicture.Transform.position = new Vector3(-2 * Screen.width, Screen.height / 2);
                gameObject.SetActive(true);

                videoPlayer.Prepare();
                videoPlayer.prepareCompleted += (source) => {
                    ///TODO 這實際上沒有辦法真的catch到錯誤,待修
                    if (videoPlayer.texture == null)
                    {
                        Logger.LogError("Video not found");
                        GameObject.Destroy(shiftPicture.Transform.parent.gameObject);
                        shiftPicture.video = null;
                        shiftPicture       = null;
                        _step = 0;
                        return;
                    }

                    shiftPicture.video.texture = videoPlayer.texture;
                    videoTimer = 2;
                    videoPlayer.Play();
                    audioSource.Play();

                    //影片太大聲QQ
                    audioSource.volume = KK_FBIOpenUp.videoVolume;

                    Left2Center();
                };
                break;
            }

            //如果影片不存在,用熊吉代替
            if (noVideoFallback)
            {
                _step = 20;
            }

            step = _step;
            Logger.LogDebug("Draw Slide Pic");

            void Right2Center()
            {
                //Right To Center
                shiftPicture.Transform.position = new Vector3(Screen.width + shiftPicture.Width / 2, Screen.height / 2);
                shiftPicture.targetPosition     = new Vector3(Screen.width / 2, Screen.height / 2);
                gameObject.SetActive(true);
            }

            void Left2Center()
            {
                //Left To Center
                shiftPicture.Transform.position = new Vector3(-1 * (Screen.width + shiftPicture.Width / 2), Screen.height / 2);
                shiftPicture.targetPosition     = new Vector3(Screen.width / 2, Screen.height / 2);
                gameObject.SetActive(true);
            }
        }
Ejemplo n.º 6
0
        private IEnumerator Initialize()
        {
            movieRenderer = targetSurface.GetComponent <Renderer>();
            if (movieRenderer == null ||
                movieRenderer.material == null ||
                movieRenderer.material.mainTexture == null)
            {
                UpdateState(StateType.Error);
                throw new Exception(TAG + " No surface for media found.");
            }
#if USE_EASY_MOVIE_TEXTURE
            // Prevent Awake called before properties loaded.
            gameObject.SetActive(false);
            // Load EasyMovieTexture properties at runtime.
            easyMovieTexture             = gameObject.AddComponent <MediaPlayerCtrl>() as MediaPlayerCtrl;
            easyMovieTexture.m_bAutoPlay = false;
            easyMovieTexture.m_bLoop     = loop;
            //easyMovieTexture.m_strFileName = videoFile;
            easyMovieTexture.m_TargetMaterial    = new GameObject[1];
            easyMovieTexture.m_TargetMaterial[0] = targetSurface;
            easyMovieTexture.m_bSupportRockchip  = true;
            easyMovieTexture.m_shaderYUV         = Shader.Find("Unlit/Unlit_YUV");
            gameObject.SetActive(true);

            easyMovieTexture.OnReady += OnPrepareComplete;
            easyMovieTexture.OnEnd   += OnVideoPlayComplete;

            easyMovieTexture.Load(videoFile);
            // Following steps will be handled by EasyMovieTexture.
            yield break;
#elif USE_UNITY_VIDEO_PLAYER
            videoPlayer                        = gameObject.AddComponent <UnityEngine.Video.VideoPlayer>();
            videoPlayer.renderMode             = UnityEngine.Video.VideoRenderMode.MaterialOverride;
            videoPlayer.targetMaterialRenderer = targetSurface.GetComponent <MeshRenderer>();

            audioSource = gameObject.AddComponent <AudioSource>();
            videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
            videoPlayer.SetTargetAudioSource(0, audioSource);

            videoPlayer.skipOnDrop = true;
            videoPlayer.isLooping  = loop;

            videoPlayer.prepareCompleted += (e) => { OnPrepareComplete(); };
            videoPlayer.loopPointReached += (e) => { OnVideoPlayComplete(); };

            yield return(null);

            string mediaUrl = "file://" + Application.streamingAssetsPath + "/" + videoFile;
#if UNITY_ANDROID && !UNITY_EDITOR
            //mediaUrl = "jar:" + mediaUrl;
            string assetsPath = Application.streamingAssetsPath + "/" + videoFile;
            string dataPath   = Application.persistentDataPath + "/" + videoFile;
            if (!File.Exists(dataPath))
            {
                WWW wwwReader = new WWW(assetsPath);
                yield return(wwwReader);

                if (wwwReader.error != null)
                {
                    Debug.LogError(TAG + " wwwReader error: " + wwwReader.error);
                }

                System.IO.File.WriteAllBytes(dataPath, wwwReader.bytes);
            }
            mediaUrl = dataPath;
#endif
            videoPlayer.url = mediaUrl;
#elif USE_ANDROID_MEDIA_PLAYER
            Media_Surface_Init();
            // TODO: Fix ArgumentException: nativeTex can not be null in Unity 2017.
            nativeTexture = Texture2D.CreateExternalTexture(
                textureWidth,
                textureHeight,
                TextureFormat.RGBA32,
                true,
                false,
                IntPtr.Zero
                );
            IssuePluginEvent(MediaSurfaceEventType.Initialize);

            string assetsPath = Application.streamingAssetsPath + "/" + videoFile;
            string dataPath   = Application.persistentDataPath + "/" + videoFile;
            if (!File.Exists(dataPath))
            {
                WWW wwwReader = new WWW(assetsPath);
                yield return(wwwReader);

                if (wwwReader.error != null)
                {
                    Debug.LogError(TAG + " wwwReader error: " + wwwReader.error);
                }

                System.IO.File.WriteAllBytes(dataPath, wwwReader.bytes);
            }

            // Delay 1 frame to allow MediaSurfaceInit from the render thread.
            yield return(null);

            mediaPlayer = GetMediaPlayerOnTextureID(textureWidth, textureHeight);

            try
            {
                mediaPlayer.Call("setDataSource", dataPath);
                mediaPlayer.Call("setLooping", loop);
            }
            catch (Exception e)
            {
                Debug.LogError(TAG + " Failed to load video for mediaPlayer: " + e.Message);
            }
#elif USE_VIVE_MEDIA_DECODER
            targetSurface.GetComponent <Renderer>().material = convertMaterial;

            mediaDecoder = targetSurface.AddComponent <HTC.UnityPlugin.Multimedia.MediaDecoder>()
                           as HTC.UnityPlugin.Multimedia.MediaDecoder;

            if (stereoSurface != null && stereoSurface2 != null)
            {
                HTC.UnityPlugin.Multimedia.StereoHandler.StereoType stereoType
                    = HTC.UnityPlugin.Multimedia.StereoHandler.StereoType.SIDE_BY_SIDE;
                if (stereoSurface.name == "UpSurface")
                {
                    stereoType = HTC.UnityPlugin.Multimedia.StereoHandler.StereoType.TOP_DOWN;
                }
                HTC.UnityPlugin.Multimedia.StereoHandler.SetStereoPair(
                    stereoSurface,
                    stereoSurface2,
                    movieRenderer.material,
                    stereoType,
                    true);
            }
            mediaDecoder.onInitComplete = new UnityEvent();
            mediaDecoder.onInitComplete.AddListener(OnPrepareComplete);
            mediaDecoder.onVideoEnd = new UnityEvent();
            mediaDecoder.onVideoEnd.AddListener(OnVideoPlayComplete);

            mediaDecoder.mediaPath = Application.streamingAssetsPath + "/" + videoFile;
            yield return(null);
#else
            audioSource = gameObject.AddComponent <AudioSource>();

            if (!Path.GetExtension(videoFile).Equals(".ogv") &&
                !Path.GetExtension(videoFile).Equals(".ogg"))
            {
                if (File.Exists(
                        Application.streamingAssetsPath + "/" +
                        Path.GetDirectoryName(videoFile) + "/" +
                        Path.GetFileNameWithoutExtension(videoFile) + ".ogv"
                        ))
                {
                    videoFile = Path.GetDirectoryName(videoFile) + "/" +
                                Path.GetFileNameWithoutExtension(videoFile) + ".ogv";
                }
                else if (File.Exists(
                             Application.streamingAssetsPath + "/" +
                             Path.GetDirectoryName(videoFile) + "/" +
                             Path.GetFileNameWithoutExtension(videoFile) + ".ogg"
                             ))
                {
                    videoFile = Path.GetDirectoryName(videoFile) + "/" +
                                Path.GetFileNameWithoutExtension(videoFile) + ".ogg";
                }
                else
                {
                    UpdateState(StateType.Error);
                    throw new Exception("Only support ogv/ogg video format on Standalone platform.");
                }
            }
            WWW wwwReader = new WWW("file:///" + Application.streamingAssetsPath + "/" + videoFile);
            yield return(wwwReader);

            if (wwwReader.error != null)
            {
                UpdateState(StateType.Error);
                Debug.LogError(TAG + " wwwReader error: " + wwwReader.error);
            }
            // Here is an issue in Unity Editor, please ignore the error if you
            // see the error below in console:
            // LoadMoveData got NULL!
            // Error: Cannot create FMOD::Sound instance for resource (null), (An invalid parameter was passed to this function. )
            //
            // Issue:
            // https://issuetracker.unity3d.com/issues/calling-www-dot-movie-fails-to-load-and-prints-error-loadmovedata-got-null
            // https://issuetracker.unity3d.com/issues/movietexture-fmod-error-when-trying-to-play-video-using-www-class
            movieTexture = wwwReader.movie;
            movieRenderer.material.mainTexture = movieTexture;
            audioSource.clip  = movieTexture.audioClip;
            movieTexture.loop = loop;
#endif
            UpdateState(StateType.Initialized);
            StartCoroutine(Prepare());
        }