unity中使用VideoPalyer播放本地視頻


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);
            }        
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM