这些天都在折腾公司的项目开发,这几天刚好有点空就把前段时间研究的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
EasyMovieTexture3.5.2:
AVPro 1.7.4: