Unity video playback realizes stepping on pit notes, which is based on FairyGUI

I contacted the native Unity VideoPlayer a few years ago and recently re contacted FGUI access. Record some pits and share them with students who need to access this area

Unity has added a new VideoPlayer in version 5.6 to replace Moive Texture. When accessing, it is best to develop the environment based on Window10 system. If it is developed based on Window7 system, there will be compatibility problems, and some platforms of the printed package will not be able to play. At present, we know that the OGV video format can be played normally on the window platform.
If you are developing based on Window10, you may want to abandon the win7 platform PC package. Some video formats are incompatible and cannot be played. If you know why, let me know.
After reading some materials, VideoPlayer is developed based on Window10 Api, so Win10 is more friendly.
Some students also encountered that some android platforms could not be played, and there were a lot of V8 settings on the Internet.
To sum up, compatibility problems and setting problems.
Unity supports different video formats and each platform has corresponding compatibility. Therefore, OGV (Ogg Theora) is recommended with small memory.

Get to the point
1. First, create FairyGUI component package and create a GLoader to prepare for the following texture

2. When VideoPlayer is mounted, calculate whether to end according to the number of frames

Insert the code slice here
    	//Write down the general steps and the ideas are as follows
    	// Update glober, texture
    	
        public GObject mainUI;
        public VideoPlayer m_videoPlayer = null;
        public AudioSource m_audioSource = null;
        public GLoader m_gLoaderVideoTexture = null;
        private float m_maxValue = 0;
        
        public override void Awake()
        {
            m_videoPlayer = Stage.inst.gameObject.GetComponent<VideoPlayer>();
            if (null == m_videoPlayer)
            {
                m_videoPlayer = Stage.inst.gameObject.AddComponent<VideoPlayer>();
            }
        }
        
		public void Update()
		{
		    if (null != mainUI && mainUI.visible)
		    {
		        if (null != m_videoPlayer && m_videoPlayer.isPlaying)
		        {
		            m_maxValue = m_videoPlayer.frameCount / m_videoPlayer.frameRate;
		            if (Mathf.Abs((int)m_videoPlayer.time - (int)m_maxValue) == 0)
		            {
		                m_videoPlayer.frame = (long)m_videoPlayer.frameCount;
		                m_videoPlayer.Stop();
		                Debug.Log("Playback complete!");
		                return;
		            }
		            if (m_videoPlayer.isPrepared)
		            {
		                m_gLoaderVideoTexture .texture = new NTexture(m_videoPlayer.texture);
		            }
		        }
		    }
		}
        public void StartVideo()
        {
            var videoClip = Resources.Load("video.mp4");
            if(null == videoClip)
            	return;
            mainUI.visible = true;
            m_videoPlayer.clip = videoClip as VideoClip;
            m_videoPlayer.playOnAwake = false;
            m_videoPlayer.isLooping = false;
            m_videoPlayer.waitForFirstFrame = true;
            m_videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
            m_videoPlayer.SetTargetAudioSource(0, m_audioSource);
            m_videoPlayer.Prepare();
            m_maxValue = m_videoPlayer.frameCount / m_videoPlayer.frameRate;

            if (!m_videoPlayer.isPrepared)
            {        
                m_videoPlayer.prepareCompleted += onPrepareFinished;
            }
            else
            {
                m_videoPlayer.Play();
            }
        }
        
        public void StopVideo()
        {
            if (null != mainUI && mainUI.visible)
            {
                if (null != m_videoPlayer)
                {
                    m_videoPlayer.Stop();
                }
                mainUI.visible = false;
            }
        }
        
        public void onPrepareFinished(VideoPlayer player)
        {
            player.Play();
        }

Specifically, if you want to achieve special playback details, Official VideoPlayer documentation:
Some pits encountered by others can be read UWA
If you are afraid of some platform compatibility problems, you can consider third-party plug-ins Avpro Video official documents
Avpro Video has strong expansibility, good compatibility and is perfect. The only thing missing is that there is no underlying source code and it is a paid plug-in.
The access implementation principle is also roughly the same. Update = > m_ gLoaderVideoTexture . texture = new NTexture(m_videoPlayer.texture);, Modify the native NGUI adaptation source code and adapt FairyGUI.

Keywords: Unity

Added by hpg4815 on Fri, 14 Jan 2022 20:01:46 +0200