unity中使用VideoPalyer播放本地视频

摘要:
使用System.Collections。通用;//图像publicRawImage_图像;privatefloatm_时间;publicGameObjectm_unFullScreenBtn;voidAwake(){//Instance=this;m_videoControlArea.SetActive(false);
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.Video;
using UnityEngine.UI;
using TMPro;
using UIWidgets;

public class VideoPlayerControl : MonoBehaviour
{
    //public static VideoPlayerControl Instance;
    //图像
    public RawImage m_image;
    //播放器
    public VideoPlayer m_vPlayer;
    //播放
    public Button m_btnPlay;
    //暂停
    public Button m_btnPause;
    public Button m_btnVolumnOn;
    public Button m_btnVolumnOff;
    //视频控制器
    public Slider m_sliderVideo;
    //音量控制器
    public Slider m_sliderSource; 
    //当前视频时间
    public Text m_textTime;
    public TextMeshProUGUI m_videoName;
    //视频总时长
    public Text m_textCount;
    //音频组件
    public AudioSource m_source;
    //需要添加播放器的物体
    public GameObject m_obj;
    //是否拿到视频总时长
    public bool m_isShow;
    //前进后退的大小
    public float m_numBer = 20f;
    //时 分的转换
    private int hour, mint;
    private float m_time;
    private float m_timeCount;
    private float m_timeCurrent;
    //视频是否播放完成
    private bool m_isVideo;
    bool isFullScreen = false;
    //隐藏视频控制按钮区域
    public GameObject m_videoControlArea;
    protected bool m_isShowUI;
    //树状图
    [SerializeField]
    public TreeView m_Tree;
    protected Vector2 m_videoAreaSize;
    protected Vector3 m_videoAreaAnchor;
    public GameObject m_fullScreenBtn;
    public GameObject m_unFullScreenBtn;
   
    void Awake()
    {
        //Instance = this;
        GetVideoName();
    }
 
    void Start()
    {
        m_image = m_obj.GetComponent<RawImage>();
        //一定要动态添加这两个组件,要不然会没声音
        m_vPlayer = m_obj.AddComponent<VideoPlayer>();
        m_source = m_obj.AddComponent<AudioSource>();

        //这3个参数不设置也会没声音 唤醒时就播放关闭
        m_vPlayer.playOnAwake = false;
        m_source.playOnAwake = false;
        m_source.Pause();
        m_videoControlArea.SetActive(false);
        //初始化
        //Init(urlNetWork);
        m_btnPlay.onClick.AddListener(delegate { OnClick(0); });
        m_btnPause.onClick.AddListener(delegate { OnClick(1); });
        m_btnVolumnOn.onClick.AddListener(delegate { OnClick(2); });
        m_btnVolumnOff.onClick.AddListener(delegate { OnClick(3); });
        m_sliderSource.value = m_source.volume;
        //text.text = string.Format("{0:0}%", source.volume * 100);
        m_sliderSource.onValueChanged.AddListener(delegate { ChangeSource(m_sliderSource.value); }); 
        //全屏的默认值
        m_videoAreaSize = m_obj.GetComponent<RectTransform>().sizeDelta;
        m_videoAreaAnchor = m_obj.GetComponent<RectTransform>().anchoredPosition3D;
    }

    void Update()
    {
        if (m_vPlayer.isPlaying && m_isShow)
        {
            //把图像赋给RawImage
            m_image.texture = m_vPlayer.texture;
            //帧数/帧速率=总时长    如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
            m_sliderVideo.maxValue = (m_vPlayer.frameCount / m_vPlayer.frameRate);
            m_time = m_sliderVideo.maxValue;
            hour = (int)m_time / 60;
            mint = (int)m_time % 60;
            m_textCount.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
            m_isShow = !m_isShow;
        }

       if (Mathf.Abs((int)m_vPlayer.time - (int)m_sliderVideo.maxValue) == 0)
        {
            m_vPlayer.frame = (long)m_vPlayer.frameCount;
            m_vPlayer.Pause();
            m_isVideo = false;
            return;
        }
        else if (m_isVideo && m_vPlayer.isPlaying)
        {
            m_time = (float)m_vPlayer.time;
            hour = (int)m_time / 60;
            mint = (int)m_time % 60;
            m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
            m_sliderVideo.value = m_time;
        }
    }
 
    /// <summary>
    /// 获取文件夹下的文件列表
    /// </summary>
    /// <param name="path"></param>
    private void GetVideoName()
    {
        string path;
        Configuration cfg = ConfigManager.GetConfig("AppConfig.xml");
        cfg.Get("VideoPath", out path);
        if (!Directory.Exists(path))
        {
            Debug.LogError("教学视频路径不存在!");
            return;
        }     
        DirectoryInfo dir = new DirectoryInfo(path);
        m_Tree.Nodes = InitTreeList(dir);
        m_Tree.gameObject.transform.Find("ScrollRect").GetComponent<ScrollRect>().horizontal = true;
    }

    /// <summary>
    /// 初始化视频文件列表
    /// </summary>
    private ObservableList<TreeNode<TreeViewItem>> InitTreeList(DirectoryInfo dir)
    {
        ObservableList<TreeNode<TreeViewItem>> nodes = new ObservableList<TreeNode<TreeViewItem>>();
        FileInfo[] files = dir.GetFiles();
        for (int i = 0; i < files.Length; i++)
        {
            FileInfo file = files[i];
            TreeViewItem item = new CustomTreeViewItem(file.Name, null,file.FullName);
            TreeNode<TreeViewItem> node = new TreeNode<TreeViewItem>(item, null);
            nodes.Add(node);
        }

        DirectoryInfo[] childDirList = dir.GetDirectories();
        for (int i = 0; i < childDirList.Length; i++)
        {
            DirectoryInfo childDir = childDirList[i];
            ObservableList<TreeNode<TreeViewItem>> childDirNodes = InitTreeList(childDir);
            TreeViewItem item = new CustomTreeViewItem(childDir.Name, null, null);
            TreeNode<TreeViewItem> childNode = new TreeNode<TreeViewItem>(item, childDirNodes);
            nodes.Add(childNode);
        }
      
        return nodes;
    }

    /// <summary>
    /// 播放视频
    /// </summary>
    /// <param name="id"></param>
    public void PlayVideo(string fullName)
    { 
        Init(fullName);//@"E:互动教学视频" +@"" 
    }

    public void OnSelect(int index, ListViewItem item)
    {
        TreeViewComponent componentItem = item as TreeViewComponent;
        CustomTreeViewItem customItem = componentItem.Item as CustomTreeViewItem;
        //Debug.Log("Selected: " + index + "; name: " + customItem.Name + "; value: " + customItem.Value + "; data: " + customItem.Data);
        if (customItem.Data != null)
        {
            Init(customItem.Data);
            m_videoName.text = customItem.Name;
        }
    }

    /// <summary>
    ///     初始化VideoPlayer
    /// </summary>
    /// <param name="url"></param>
    private void Init(string url)
    {
        m_isVideo = true;
        m_isShow = true;
        m_timeCount = 0;
        m_timeCurrent = 0;
        m_sliderVideo.value = 0;
        //设置为URL模式
        m_vPlayer.source = VideoSource.Url;
        //设置播放路径
        m_vPlayer.url = url;
        //在视频中嵌入的音频类型
        m_vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

        //把声音组件赋值给VideoPlayer
        m_vPlayer.SetTargetAudioSource(0, m_source);
        m_vPlayer.controlledAudioTrackCount = 1;
        m_vPlayer.IsAudioTrackEnabled(0);
        //当VideoPlayer全部设置好的时候调用
        m_vPlayer.prepareCompleted += Prepared;
        //启动播放器
        m_vPlayer.Prepare();
       // player.Play();
    }

    /// <summary>
    ///     改变音量大小
    /// </summary>
    /// <param name="value"></param>
    public void ChangeSource(float value)
    {
        m_source.volume = value;
       // text.text = string.Format("{0:0}%", value * 100);
    }

  
    /// <summary>
    ///     改变视频进度
    /// </summary>
    /// <param name="value"></param>
    public void ChangeVideo()
    {   
        if (m_vPlayer.isPrepared)
        {
            m_vPlayer.time = (long)m_sliderVideo.value;

            //Debug.Log((long)value);
            //Debug.Log("VideoPlayer Time:" + m_vPlayer.time);
            m_time = (float)m_vPlayer.time;
            hour = (int)m_time / 60;
            mint = (int)m_time % 60;
            m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
        }
        if (m_isVideo == false)
        {
            m_isVideo = true;
        }
        m_vPlayer.Play();
    }

    private void OnClick(int num)
    {
        switch (num)
        {
            case 0:
                m_vPlayer.Play();
                m_btnPlay.gameObject.SetActive(false);
                m_btnPause.gameObject.SetActive(true);
                Time.timeScale = 1;
                break;
            case 1:
                m_vPlayer.Pause();
                m_btnPlay.gameObject.SetActive(true);
                m_btnPause.gameObject.SetActive(false);
                Time.timeScale = 0;
                break;
            case 2:
                m_source.volume = 0;            
                m_btnVolumnOn.gameObject.SetActive(false);
                m_btnVolumnOff.gameObject.SetActive(true);
                break;
            case 3:
                m_source.volume = m_sliderSource.value;
                m_btnVolumnOn.gameObject.SetActive(true);
                m_btnVolumnOff.gameObject.SetActive(false);
                break;          
            default:
                break;
        }
    }

    void Prepared(VideoPlayer player)
    {
        player.Play();
    }

    /// <summary>
    /// 当鼠标进入到下方进度条区域时显示进度条
    /// </summary>
    public void OnControllAreaPointEnter()
    {
        if (!m_isShowUI)
        {
            m_videoControlArea.SetActive(true);
            m_isShowUI = true;
        }
    }

    /// <summary>
    /// 当鼠标离开下方进度条区域时隐藏进度条
    /// </summary>
    public void OnControllAreaPointExit()
    {
        if (m_isShowUI)
        {
            m_videoControlArea.SetActive(false);
            m_isShowUI = false;
        }
    }

    public void OnFullScreenBtnClick()
    {
        if (!isFullScreen && m_vPlayer.isPrepared)
        {
            var rt = m_image.GetComponent<RectTransform>();
            RectTransform fullScreen = GameObject.Find("MainUI").GetComponent<RectTransform>();
            //m_image.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width, Screen.height);
            rt.localScale = new Vector3(fullScreen.rect.width / rt.rect.width, fullScreen.rect.height / rt.rect.height, 0);
            rt.position = fullScreen.position;
            isFullScreen = true;
            m_fullScreenBtn.SetActive(false);
            m_unFullScreenBtn.SetActive(true);
        }
        else
        {
            m_image.GetComponent<RectTransform>().localScale = Vector3.one;
            m_image.GetComponent<RectTransform>().anchoredPosition3D = m_videoAreaAnchor;
            isFullScreen = false;
            m_fullScreenBtn.SetActive(true);
            m_unFullScreenBtn.SetActive(false);
        }

    }

    public void OnSelectTab(int id)
    {
        if (id == 4)
        {
            //m_tree.SelectNode(m_tree.DataSource[0].Node);
            if (m_Tree.SelectedNode != null)
            {
                m_Tree.DeselectNode(m_Tree.SelectedNode);
            }        
        }
    }

}

免责声明:文章转载自《unity中使用VideoPalyer播放本地视频》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue.js学习笔记(2)— sessionStorage存储和获取数据Unity 中的坐标系下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c语言_头文件_windows.h

概述 Win32程序的开头都可看到: #include <windows.h> WINDOWS.H是一个最重要的头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件。这些头文件中最重要的和最基本的是: WINDEF.H 基本数据类型定义。 WINNT.H 支持Unicode的类型定义。 WINBASE.H Kernel(...

稍为改写了下DropBrute用于IPV6检测nginx的access_log

#!/bin/sh # # DropBrute.sh @20130516 # # minimalist OpenWRT/dropbear ssh brute force attack banning script # # Installation steps: # # 1) Optionally edit the variables in the head...

WPF整理-使用用户选择主题的颜色和字体

“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows Control Panel Personalization applet (or the older Display Settings in Windows...

[渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署

这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第五篇:MVC程序中实体框架的Code First迁移和部署 原文:Code First Migrations and Deployment with the Entity F...

DB2中Lob is closed. ERRORCODE=4470的解决

使用DB2的Blob类型是,出现一下错误: Lob is closed. ERRORCODE=-4470, SQLSTATE=null;?C:com.ibm.db2.jcc.b.SqlException: [jcc][10120][11936][3.50.152] 操纵无效:已封闭 Lob。 ERRORCODE=-4470, SQLSTATE=null。...

C#调用开源图像识别类库tessnet2

首先下载tessnet2_32.dll及相关语言包,将dll加入引用   使用方法很简单 1.下载tesseract 的.net 类库tessnet2_32.dll ,添加引用。   http://www.pixel-technology.com/freeware/tessnet2/ 2.下载tesseract 相对应的语言包。 http://code....