Unity视频读取

视频单个读取StreamingAssets文件夹中的视频

Unity视频读取_第1张图片
1569381686(1).png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;

public class VideoControl : MonoBehaviour {
    public static VideoControl instance;
    public Image waittingPage;
    public MediaPlayer m_mediaPlayer;
    public string videoName;

    // Use this for initialization
    private void Awake()
    {
        instance = this;
    }
    void Start () {
    }

    // Update is called once per frame
    void Update () {

        if (m_mediaPlayer.Control.IsFinished())
        {

            m_mediaPlayer.transform.parent.gameObject.SetActive(false);
            Debug.Log("11");
            waittingPage.gameObject.SetActive(true);
            m_mediaPlayer.Control.Stop();

        }

        if (m_mediaPlayer.Control.IsPlaying())
        {
            m_mediaPlayer.m_AutoStart = false;
            m_mediaPlayer.m_AutoOpen = false;
        }
    }

    void OnEnable()
    {
        OnOpenVideoFile(videoName);
        print(m_mediaPlayer.Control.IsPlaying());
        m_mediaPlayer.Control.Play();
        m_mediaPlayer.m_AutoStart = false;
        m_mediaPlayer.m_AutoOpen = false;
        Debug.Log("11111111");
        m_mediaPlayer.Control.Play();
    }

    public void closeMedia()
    {
        m_mediaPlayer.transform.parent.gameObject.SetActive(false);
    }

    public void OnOpenVideoFile(string videoname)
    {
        m_mediaPlayer.m_VideoPath = string.Empty;
        print(videoname);
        m_mediaPlayer.m_VideoPath = videoname + ".mp4";
        if (string.IsNullOrEmpty(m_mediaPlayer.m_VideoPath))
        {
            m_mediaPlayer.CloseVideo();
            
        }
        else
        {
            m_mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, m_mediaPlayer.m_VideoPath, false);

        }
    }
}



视频多个按顺序读取StreamingAssets文件夹

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using System.IO;

public class VideoControl : MonoBehaviour {
    public static VideoControl instance;
    public Image waittingPage;
    public MediaPlayer m_mediaPlayer;
    public string videoName;

    private List videoNameList = new List();
    string dirPath;
    // Use this for initialization
    private void Awake()
    {

        instance = this;
        dirPath = Application.streamingAssetsPath + "/Video";
        GetAllFileName();

    }

    int index = 0;
    string currentName;
    void Update () {

        if (m_mediaPlayer.Control.IsFinished())
        {

            m_mediaPlayer.Control.Stop();
            OnOpenVideoFile();
            m_mediaPlayer.Control.Play();

        }

        if (m_mediaPlayer.Control.IsPlaying())
        {

            m_mediaPlayer.m_AutoStart = false;
            m_mediaPlayer.m_AutoOpen = false;

        }
    }

    void OnEnable()
    {
        OnOpenVideoFile();
        m_mediaPlayer.m_AutoStart = false;
        m_mediaPlayer.m_AutoOpen = false;
        m_mediaPlayer.Control.Play();
    }
    

    public void OnOpenVideoFile()
    {
        if (videoNameList.Count<=0)
        {
            return;
        }
    //痛点破局点的敏锐之眼   同理心   动手优化改善   需求   感知层,角色框架层 
        currentName = videoNameList[index];
        index++;
        if (index >= videoNameList.Count)
        {
            index = 0;
        }
        m_mediaPlayer.m_VideoPath = string.Empty;

        m_mediaPlayer.m_VideoPath =Application.streamingAssetsPath+ "/Video/" + currentName;
        print(m_mediaPlayer.m_VideoPath);
        if (string.IsNullOrEmpty(m_mediaPlayer.m_VideoPath))
        {

            m_mediaPlayer.CloseVideo();
            
        }
        else
        {
           
            m_mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, m_mediaPlayer.m_VideoPath, false);
           
        }
    }

    void GetAllFileName() {

        DirectoryInfo dir = new DirectoryInfo(dirPath);

        FileInfo[] files = dir.GetFiles(); //获取所有文件信息

        foreach (var file in files)
        {
            if (file.Name.EndsWith(".meta"))
            {
                continue;
            }
            print(file.Name);
            videoNameList.Add(file.Name);
        }

     }

}

你可能感兴趣的:(Unity视频读取)