unity 播放视频

 

2019-02-20 11:46:46

补充:这篇文章写得时候用的unity5.4,2017,2018的版本官方已经提供了相应的video播放功能!

最近要调研unity中播放在移动端播放视频的插件,开一篇博客来记录
1.Handheld.PlayFullScreenMovie
Unity标准的视频播放接口,支持的播放视频格式有.mov、.mpg、.mpeg、.mp4、.avi和.asf。
1)仅能全屏
2)在移动端均能正常播放,在pc没什么反应
3)视频播放时,unity暂停
相关博客:
http://www.weixinnu.com/tag/article/2878539837(unity播放的各种实现方式)
http://www.xuanyusong.com/archives/1019(Unity3D研究院之两种方式播放游戏视频
http://blog.csdn.net/liangzg_2011/article/details/46373723(检测视频是否播放结束)

http://blog.csdn.net/zimucderock/article/details/53374923(Handheld.PlayFullScreenMovie播放视频参数)

 

给出我当前项目中使用的代码

public static void PlayVideo(string str)
    {
        //沙盒目录存在就播沙盒,不存在就播SA目录
        str = str + ".mp4";
        string arg = "assetbundle/video/";
        string realStr = Application.persistentDataPath + "/" + arg + str;
        if (File.Exists(realStr))
        {
#if UNITY_IOS
        realStr = "file:///" + realStr;
#else
#endif
        }
        else
        {
            realStr = arg + str;
        }
        Handheld.PlayFullScreenMovie(realStr, Color.black, FullScreenMovieControlMode.CancelOnInput);
    }


对于IOS11以上 FullScreenMovieControlMode.CancelOnInput点击不能取消播放的问题,参考我另一片博文

:http://blog.csdn.net/u010314160/article/details/79088166

 

2.EasyMovieTexture

学习下载地址:http://www.drv5.cn/sfinfo/14655.html

相关博客:

http://blog.csdn.net/begonia__z/article/details/51546267(EasyMovieTexture视频播放插件使用浅谈)

先给出测试结果,在PC和移动端均能播放

接入时遇到的部分问题:

1) untiy unsafe 问题,导入插件(3.57版本)时,遇到次报错,解决方案:http://blog.csdn.net/u010314160/article/details/79198725

2)接入时的使用,我项目中想用要用到的是全屏的模式,Demo中FullScreen预制直接拿来用,放到项目中的预制资源目录,新建脚本VideoManager.cs来负责视屏播放的功能,代码给出

using UnityEngine;
using System.Collections;
using System.IO;
using WaterBell.ProjX.Core.Manager.Audio;

public class VideoManager : MonoBehaviour {

    public MediaPlayerCtrl scrMedia;
    private static VideoManager instance = null;

    Camera camer;
    MovieControlMode mode = MovieControlMode.Full;
    private bool Playing = false;

    public enum MovieControlMode
    {
        Full = 0,//不可控制
        CancelOnInput = 1,//点击关闭
    }

    public static VideoManager GetInstance()
    {
        if (instance == null)
        {
           GameObject obj = AssetsManager.LoadResources("UI/Prefab/VideoManager");
           obj = Instantiate(obj);
           instance = obj.GetComponent();
        }
        
        return instance;
    }
	
    void Start()
    {
        instance = this;

        //camer = transform.GetComponent();
        //camer.enabled = false;

        scrMedia.OnEnd += OnEnd;
    }

    public void Load(string str,VideoManager.MovieControlMode mode = MovieControlMode.CancelOnInput)
    {
        str = str + ".mp4";
        string realStr;
#if UNITY_EDITOR
        realStr = Application.dataPath + "/Video/" + str;
        if (!File.Exists(realStr))
        {
            Debug.LogError("Video不存在:" + realStr);
            return;
        }
        realStr = "file://" + realStr;
#else  
        string arg = "assetbundle/video/";
        realStr = Application.persistentDataPath + "/" + arg + str;
        if (!File.Exists(realStr))
        {
            realStr = arg + str;
        }
        else
        {
            realStr = "file://" + realStr;
        }
#endif
        Debug.Log("Video路径:" + realStr);
        SoundControl();
        scrMedia.Load(realStr);
        //camer.enabled = true;
        this.mode = mode;
        Playing = true;
    }

    void SoundControl()
    {
        AudioManager.GetInstance().SoundVolume = AudioManager.GetInstance().SoundVolume > 0f ? 0f : AudioManager.MAX_SOUND_VOL;
        AudioManager.GetInstance().MusicVolume = AudioManager.GetInstance().MusicVolume > 0f ? 0f : AudioManager.MAX_MUSIC_VOL;
    }

    public void UnLoad()
    {
        scrMedia.UnLoad();
        Debug.Log("Video UnLoad");
    }

    void OnEnd()
    {
        //camer.enabled = false;
        Playing = false;
        UnLoad();
        SoundControl();
        if (gameObject != null)
        {
            GameObject.Destroy(gameObject);
        }
    }

    void Update()
    {
        if (!Playing)
        {
            return;
        }
        if (mode != MovieControlMode.CancelOnInput)
        {
            return;
        }

#if UNITY_EDITOR
        if (Input.GetMouseButtonUp(0))
        {
            OnEnd();
        }
#else
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            OnEnd();
        }
#endif

    }
}

 

 

播放视频:VideoManager GetInstance().Load("test");//默认mp4格式

MovieControlMode:是否可点击关闭。

路径根据自己的需求调整,代码很简单,不多解释了。

全屏预制中已经包含了一个相机,设置下深度值(约定?),确保打开相机的时候没有其他场景即可。
 

3.AVPro Video

官方网站:http://renderheads.com/product/avpro-video 网站内提供了免费版

相关博客:

http://blog.csdn.net/dark00800/article/details/56015917(AVPro Video的使用)

 
 
 
 
 
 
 
 
 

 

你可能感兴趣的:(Unity)