Unity3D學習日記(九) EasyMovieTexture視訊播放外掛使用淺談
阿新 • • 發佈:2019-02-09
這些天都在折騰公司的專案開發,這幾天剛好有點空就把前段時間研究的EasyMovieTexture外掛拿來寫寫部落格,這個外掛估計不少人用的挺多的吧。因為Unity3D可以用到移動端的視訊外掛在國內也就是那幾個而已,其中有MMTexture、AVI Player、以及EasyMovieTexture。(PS:有錢的公司可以直接去買那個貴的離譜的視訊外掛,聽說還不錯)之前我在AVI Player的文章裡面說到了EasyMovieTexture不知道為啥全黑不能用,後來我發現這個外掛在移動端上是可以正常使用的。(PS:Ios和安卓都測試過了可以使用)
這裡我用UGUI做了一個簡單的視訊播放器介面,介面就如下所示:
using UnityEngine; using System.Collections; using UnityEngine.UI; public class UIMovieEasyMovieTexture : MonoBehaviour { public Text totalTime; public Text movieTime; public Slider movieSlider; public Material renderMaterial; public int movieSpeed = 1000; private MediaPlayerCtrl mediaPlayerCtrl; private RawImage movieRawImage; private Vector2 reducteOffsetMax; private bool isFinish = false; private bool isPlay = false; private bool isCtrlMovie = false; private int fastbackTemp = 0; private int totaltime = 0; // Use this for initialization void Start () { Init(); mediaPlayerCtrl.OnEnd += OnEnd; } /// <summary> /// 初始化函式 /// </summary> public void Init() { mediaPlayerCtrl = this.transform.GetComponent<MediaPlayerCtrl>(); movieRawImage = this.transform.GetComponent<RawImage>(); mediaPlayerCtrl.m_bAutoPlay = false; if (movieRawImage.material == null) movieRawImage.material = renderMaterial; reducteOffsetMax = movieRawImage.rectTransform.offsetMax; } // Update is called once per frame void Update () { UpdateMovieSlider(); UpdateMovieTime(); UpdateTotalTime(); } /// <summary> /// 更新視訊進度條 /// </summary> public void UpdateMovieSlider() { if (mediaPlayerCtrl.GetSeekPosition() != 0 && !isCtrlMovie) { float slider = (float) mediaPlayerCtrl.GetSeekPosition()/(float) mediaPlayerCtrl.GetDuration(); movieSlider.value = slider; } } /// <summary> /// 更新視訊總時間 /// </summary> public void UpdateTotalTime() { if (this.totalTime.text != null && isPlay) { if (totaltime != mediaPlayerCtrl.GetDuration() / 1000) { totaltime = mediaPlayerCtrl.GetDuration() / 1000; int min = totaltime / 60; int seconds = totaltime - min * 60; this.totalTime.text = " / " + min + ":" + seconds; } } } /// <summary> /// 更新視訊時間 /// </summary> public void UpdateMovieTime() { if (this.movieTime.text != null && isPlay) { int movieTime = mediaPlayerCtrl.GetSeekPosition() / 1000; int min = movieTime / 60; int seconds = movieTime - min * 60; this.movieTime.text = min + ":" + seconds; } } /// <summary> /// 視訊播放 /// </summary> public void MoviePlay() { mediaPlayerCtrl.Play(); isPlay = true; isFinish = false; } /// <summary> /// 視訊暫停 /// </summary> public void MoviePause() { isPlay = false; mediaPlayerCtrl.Pause(); } /// <summary> /// 視訊停止 /// </summary> public void MovieStop() { isPlay = false; mediaPlayerCtrl.Stop(); } /// <summary> /// 讀取視訊 /// </summary> /// <param name="namePtah">讀取視訊的路徑</param> public void MovieLoad(string namePtah) { mediaPlayerCtrl.Load(namePtah); mediaPlayerCtrl.m_bAutoPlay = false; isPlay = false; isFinish = false; } /// <summary> /// 視訊設定迴圈播放 /// </summary> public void MovieLoop() { mediaPlayerCtrl.m_bLoop = !mediaPlayerCtrl.m_bLoop; } /// <summary> /// 視訊全屏顯示 /// </summary> public void OnClickFullScreen() { if (mediaPlayerCtrl.m_bFullScreen) { mediaPlayerCtrl.m_bFullScreen = false; movieRawImage.rectTransform.offsetMax = reducteOffsetMax; movieRawImage.rectTransform.offsetMin = -reducteOffsetMax; } else { mediaPlayerCtrl.m_bFullScreen = true; movieRawImage.rectTransform.offsetMax = new Vector2(0, 0); movieRawImage.rectTransform.offsetMin = new Vector2(0, 0); } } /// <summary> /// 關閉視訊介面 /// </summary> public void OnClickClose() { mediaPlayerCtrl.Stop(); movieRawImage.gameObject.SetActive(false); } /// <summary> /// 移動視訊進度條 /// </summary> public void OnDragSlider() { if (!isPlay) return; int seekPos = (int)(movieSlider.value * mediaPlayerCtrl.GetDuration()); mediaPlayerCtrl.SeekTo(seekPos); } /// <summary> /// 按下視訊進度條 /// </summary> public void OnPoniterDownSlider() { if (!isPlay) return; MoviePause(); isCtrlMovie = true; int seekPos = (int)(movieSlider.value * mediaPlayerCtrl.GetDuration()); mediaPlayerCtrl.SeekTo(seekPos); } /// <summary> /// 彈起視訊進度條 /// </summary> public void OnPoniterUpSlider() { if(!isPlay) return; MoviePlay(); isCtrlMovie = false; } public bool GetIsFullScreen() { return mediaPlayerCtrl.m_bFullScreen; } public bool GetIsLoop() { return mediaPlayerCtrl.m_bLoop; } void OnEnd() { isFinish = true; } }
把這個指令碼放到RawImage組建裡面,如圖所示:
接著我們要在要改改RawImage的錨點設定,改成全屏自動拉伸的錨點,如圖所示:
然後我們為Slider元件新增事件監聽器,用來控制視訊的快進和倒退,詳細的如圖所示:
還有些按鈕的事件新增這裡就不說了,EasyMovieTexture的播放器就全部製作完畢了。
這個外掛我還深入的去研究的,如果有興趣可以私信我一起討論
EasyMovieTexture 3.21開始支援PC端的說,雖然作者還在測試階段,但是普遍網上能找到的都是2.36版本,所以不久的將來就會有穩定版的PC端了吧~...但是本屌絲並沒有這個最新版本的說...如果有人願意分享一個給我...我很開心的說...這裡說一下2.36版本mp4和mov格式的視訊是支援的..但是mvk和ogv格式的是不行的(PS:ogv是別的朋友測試的)
2016/8/31
今天非常幸運找到了3.20版本的外掛,有需要的朋友可以私信我喲
2018/3//19