視頻單個讀取StreamingAssets文件夾中的視頻
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<string> videoNameList = new List<string>();
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;
}
//痛點破局點的敏銳之眼 同理心 動手優(yōu)化改善 需求 感知層默垄,角色框架層
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);
}
}
}