Unity中實現VideoPalyer控件操作本地視頻


一、搭建基礎的視頻顯示UI(其中時間進度條、音量進度條、及其對應的顯示文字UI可以不用搭建)

 

 

 

 

 

注意:要想實現鼠標移入顯示進度條、音量和時間控件,需要給視頻顯示控件RawImage添加BoxCollider 2D碰撞體,且調整碰撞體大小為自己想要觸發的區域即可。 

二、編寫視頻控制腳本

/

/***
*    Title:"可視化" 項目
*    主題:視頻播放控制
*    Description:
*    功能:
*    1、加載本地視頻進行播放、暫停、停止
*    2、獲取視頻總時長、當前播放時長(對應進度條)
*    3、實現進度條控制視頻音量大小
*    4、實現鼠標進入顯示進度條、音量和時間控件
*    5、實現鼠標退出隱藏進度條、音量和時間控件
*    Date:2019
*    Version:0.1版本
*    Author:Coffee
*    Modify Recoder:
*/

using Global;
using kernal;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace Control
{
public class Ctrl_VideosOperation : MonoBehaviour
{
#region 視頻組件

#region 基礎組件
//視頻播放組件
private VideoPlayer videoPlayer; //視頻組件
private AudioSource audioSource; //音頻組件
private RawImage rawImage; //顯示組件
public bool satrtplay = false; //開始播放視頻開關
private string videoPath = string.Empty; //視頻路徑

#endregion

#region 拓展組件
//視頻控制器
public Slider m_sliderVideo;
//音量控制器
public Slider m_sliderSource;
//視頻總時長
public Text m_textCount;
//當前視頻時間
public Text m_textTime;
//是否拿到視頻總時長
public bool m_isShow;
//視頻是否播放完成
private bool m_isVideo;
//時 分的轉換
private int hour, mint;
private float m_time;
private float m_timeCount;
private float m_timeCurrent;

#endregion

#endregion

void Awake()
{
//初始化配置
InitSettings();

}

private void Start()
{
if (m_sliderSource != null)
{
//調節音量方法
m_sliderSource.onValueChanged.AddListener(delegate { ChangeSource(m_sliderSource.value); });
}

}

private void Update()
{
if (satrtplay && videoPlayer.isPlaying)
{
rawImage.texture = videoPlayer.texture;
if (m_sliderVideo!=null)
{
//幀數/幀速率=總時長 如果是本地直接賦值的視頻,我們可以通過VideoClip.length獲取總時長
m_sliderVideo.maxValue = (videoPlayer.frameCount / videoPlayer.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;
m_isVideo = false;
}

}

if (satrtplay)
{
if (m_sliderVideo!=null)
{
if (Mathf.Abs((int)videoPlayer.time - (int)m_sliderVideo.maxValue) == 0)
{
videoPlayer.frame = (long)videoPlayer.frameCount;
videoPlayer.Pause();
m_isVideo = true;
satrtplay = false;
hour = 0;
mint = 0;
return;
}
else if (videoPlayer.isPlaying)
{
m_time = (float)videoPlayer.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;
}
}

}

}


#region 公有方法

/// <summary>
/// 加載封面
/// </summary>
public void LoadFirstPicture()
{
Play();
Invoke("Pause", Global_Parameter.INTERVAL_TIME_0DOT2);
}

/// <summary>
/// 獲取本地視頻
/// </summary>
/// <param name="videoPath">視頻路徑</param>
/// <returns></returns>
public bool LoadVideo(string videoPath)
{
bool success = false;
if (!string.IsNullOrWhiteSpace(videoPath))
{
//設置視頻組件格式
videoPlayer.source = VideoSource.Url;

//獲取視頻
videoPlayer.url = "file:///" + videoPath;
this.videoPath = videoPath;
success = true;

}
return success;
}

/// <summary>
/// 播放
/// </summary>
/// <returns>false:表示沒有指定裝載視頻(或裝載失敗)</returns>
public bool Play()
{
bool success = false;
if (!string.IsNullOrWhiteSpace(videoPath))
{
//設置音效大小
audioSource.volume = Global_Parameter.INTERVAL_TIME_0DOT5;
if (m_sliderSource!=null)
{
m_sliderSource.value = audioSource.volume;
}

videoPlayer.Play();
success = true;
}
return success;
}

/// <summary>
/// 暫停
/// </summary>
/// <returns>false:表示沒有指定裝載視頻(或裝載失敗)</returns>
public bool Pause()
{
bool success = false;
if (!string.IsNullOrWhiteSpace(videoPath))
{
videoPlayer.Pause();
success = true;
}
return success;
}


/// <summary>
/// 停止
/// </summary>
/// <returns>false:表示沒有指定裝載視頻(或裝載失敗)</returns>
public bool Stop()
{
bool success = false;
if (!string.IsNullOrWhiteSpace(videoPath))
{
videoPlayer.Stop();
success = true;
// 加載封面
LoadFirstPicture();
}
return success;
}

/// <summary>
/// 改變視頻進度
/// </summary>
/// <param name="value"></param>
public void ChangeVideo()
{
if (videoPlayer.isPrepared)
{
videoPlayer.time = m_sliderVideo.value;

//Debug.Log((long)value);
//Debug.Log("VideoPlayer Time:" + m_vPlayer.time);
m_time = (float)videoPlayer.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;
}
}

#endregion



#region 私有方法

/// <summary>
/// 初始化配置
/// </summary>
private void InitSettings()
{
//添加音頻、視頻組件
audioSource = this.gameObject.AddComponent<AudioSource>();
videoPlayer = this.gameObject.AddComponent<VideoPlayer>();
rawImage = this.gameObject.GetComponent<RawImage>();
//這3個參數不設置也會沒聲音 喚醒時就播放關閉
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
audioSource.volume = 0;
audioSource.Pause();

}

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

/// <summary>
/// 鼠標進入顯示視頻拓展內容(顯示進度條、音量和時間)
/// </summary>
private void OnMouseEnter()
{
ShowVideoExternInfo(true);
}

/// <summary>
/// 鼠標退出顯示視頻拓展內容(隱藏進度條、音量和時間)
/// </summary>
private void OnMouseExit()
{
ShowVideoExternInfo(false);
}


//是否顯視頻的拓展內容(顯示進度條、音量和時間)
private void ShowVideoExternInfo(bool isshow=false)
{
if (m_sliderSource != null && m_sliderVideo != null && m_textCount != null && m_textTime != null)
{
m_sliderSource.gameObject.SetActive(isshow);
m_sliderVideo.gameObject.SetActive(isshow);
m_textCount.gameObject.SetActive(isshow);
m_textTime.gameObject.SetActive(isshow);
}
}

#endregion



}//Class_end
}

 


三、使用方法

 ①將該設備播放控制腳本(Ctrl_VideosOperation.cs)添加給視頻顯示組件(我這里是:Image_Videos (3))

  

 

 

 

②新建一個空物體(取名:_TestVideosOperation)作為測試視頻操作腳本的掛載物體(Test_VideosOpreation.cs)

  

 

 

③測試視頻操作腳本的掛載物體(Test_VideosOpreation.cs)如下:

/***
*    Title:"可視化" 項目
*    主題:測試視頻播放控制
*    Description:
*    功能:XXX
*    Date:2019
*    Version:0.1版本
*    Author:Coffee
*    Modify Recoder:
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Control;
using System.IO;
using kernal;
using Global;
using UnityEngine.EventSystems;

namespace TestInfo
{
public class Test_VideosOpreation : MonoBehaviour
{
public GameObject showRawImage; //顯示面板
private Ctrl_VideosOperation _VideosOperation; //視頻控制腳本



void Start()
{
if (showRawImage!=null)
{
_VideosOperation = showRawImage.GetComponent<Ctrl_VideosOperation>();
}

//獲得本地的視頻路徑
string _VideoPath=@"C:\測試視頻\第1章 基本概念清晰版.mp4";

_VideosOperation.satrtplay = _VideosOperation.LoadVideo(VideoPath);
_VideosOperation.LoadFirstPicture();

}


void Update()
{
//開始播放
if (Input.GetKeyDown(KeyCode.P))
{
_VideosOperation.Play();
}

//暫停播放
if (Input.GetKeyDown(KeyCode.L))
{
_VideosOperation.Pause();
}

//停止播放
if (Input.GetKeyDown(KeyCode.S))
{
_VideosOperation.Stop();
}

}


}//Class_end
}

 


④運行測試,效果圖如下:

 

 

 

 

 

 

 

 

 

 

參考:unity中使用VideoPalyer播放本地視頻

 
————————————————
版權聲明:本文為CSDN博主「xiaochenXIHUA」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xiaochenXIHUA/java/article/details/92029442


免責聲明!

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



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