unity移动(安卓端播放视频)使用插件EasyMovieTexture

使用插件:EasyMovieTexture

用于播放视频,可播放的格式为.mp4   .mov  不可播放的格式为.ogv格式

使用方式:

将Prefab中VideoManager预设体拖入场景中,去掉B Loop和B Auto Play(视频循环播放和视频自动播放)

编写代码:功能为 点击按钮播放视频

视频播放控制:

由于Load方法会花费一定的时间,当没有加载视频没有完成时,点击按钮视频不会播放,但是当在unity编辑模式失去焦点时,会重新播放(为制作时发现的一个插件BUG)

解决方案:加载完成时会调用委托OnReady方法,因此将LoadFinished作为回调,加载完成后即播放视频。

using UnityEngine;

/// 
/// 视频播放插件控制脚本
/// 
public class MobileVideoPlay : MonoBehaviour {

    public MediaPlayerCtrl m_MediaPlayerCtrl;

    public void PlayMobileVideo(string videoName)
    {
        if(m_MediaPlayerCtrl.GetCurrentState() == MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING)
        {
            m_MediaPlayerCtrl.Stop();
        }
        m_MediaPlayerCtrl.UnLoad();


        m_MediaPlayerCtrl.Load(videoName);
        m_MediaPlayerCtrl.OnReady = LoadFinished;
    }

    public void LoadFinished()
    {
        m_MediaPlayerCtrl.Play();
    }
}

按钮控制:

using UnityEngine;
using UnityEngine.UI;

/// 
/// 测试播放视频
/// 
public class TestVideoPlay : MonoBehaviour {

    private void Start()
    {
        GetComponent

 

点击按钮播放下一个视频:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UIRawImageMoviePlayControl : MonoBehaviour
{
    private MediaPlayerCtrl mediaPlayerCtrl;
    private RawImage movieRawImage;
 
    private int currentPlayMovieIndex;
    private string[] needPlayMovieNameArr = new string[] { "EasyMovieTexture.mp4", "ed_1024_512kb.mp4", "TPJ_2_xianxiang.mov" };

 
    void Start()
    {
        mediaPlayerCtrl = GetComponent();
        movieRawImage = GetComponent();
 
        Material movieMaterial = Instantiate(Resources.Load("MovieMaterial")) as Material;
        movieRawImage.material = movieMaterial;
 
        mediaPlayerCtrl.m_bAutoPlay = false;
 
        currentPlayMovieIndex = 0;
    }
 
    public void OnPlayBtnClick()
    {
        movieRawImage.gameObject.SetActive(true);
 
        mediaPlayerCtrl.Load(needPlayMovieNameArr[currentPlayMovieIndex]);
 
        mediaPlayerCtrl.Play();
    }
 
    public void OnNextBtnClick()
    {
        OnStopBtnClick();
 
        if (currentPlayMovieIndex < needPlayMovieNameArr.Length - 1) currentPlayMovieIndex++;
        else currentPlayMovieIndex = 0;
 
        OnPlayBtnClick();
    }
 
    public void OnPauseBtnClick()
    {
        mediaPlayerCtrl.Pause();
    }
 
    public void OnStopBtnClick()
    {
        mediaPlayerCtrl.Stop();
        movieRawImage.gameObject.SetActive(false);
    }
}

 

补充内容:

场景中有Canvas界面,如果希望将VideoManager显示在制定UI界面前,由于Canvas处于最前面,遮挡VideoManager

具体解决做法为:

将Canvas中Render Mode 更改为World Space,此时调整z轴时可以更改遮挡关系

你可能感兴趣的:(Unity记录内容)