unity版本 2019.2.0
使用组件 VideoPlayer
-
创建一个panel ,添加RawImage
-
在panel添加VideoPlayer组件
这里我用url方式获得视频,将视频链接填入URL栏,这时候视频可以播放,但是没有声音
3. 在panel上添加AudioSource组件,将audiosource拖到videoplayer组件里面
4. 下面是测试代码,有静音和视频倒计时
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoPlayerTest: MonoBehaviour
{
enum AudioState
{
Open,
Mute
}
VideoPlayer videoPlayer;
/// <summary>
/// 静音按钮,点击切换状态
/// </summary>
public Button muteBtn;
/// <summary>
/// 当前静音状态
/// </summary>
AudioState currstate;
/// <summary>
/// 静音/未静音图标
/// </summary>
public Sprite soundOn, soundOff;
/// <summary>
/// 显示倒计时的文本
/// </summary>
public Text countDownTimeTerxt;
void Start()
{
videoPlayer = transform.GetComponent<VideoPlayer>();
videoPlayer.playOnAwake = false;
videoPlayer.isLooping = false;
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
// 设置音频输出模式, 否则无法设置音量
videoPlayer.audioOutputMode = VideoAudioOutputMode.Direct;
RawImage rawImage = GetComponent<RawImage>();
videoPlayer.targetTexture = creatTexture();
rawImage.texture = videoPlayer.targetTexture;
//videoPlayer.url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
videoPlayer.url = "https://media.w3.org/2010/05/sintel/trailer.mp4";
muteBtn.onClick.AddListener(setMuteVolume);
StartCoroutine(VideoPlay());
}
/// <summary>
/// 静音
/// </summary>
void setMuteVolume()
{
//Debug.Log("静音" + videoPlayer.GetDirectAudioVolume(0));
//videoPlayer.SetDirectAudioVolume(0, 0);
if (currstate == AudioState.Mute)
{
muteBtn.image.sprite = soundOn;
videoPlayer.SetDirectAudioMute(0, false);
}
else
{
muteBtn.image.sprite = soundOff;
videoPlayer.SetDirectAudioMute(0, true);
}
currstate = videoPlayer.GetDirectAudioMute(0) ? AudioState.Mute : AudioState.Open;
}
RenderTexture texture;
RenderTexture creatTexture()
{
texture = RenderTexture.GetTemporary(Screen.width, Screen.height);
//texture = new RenderTexture(Screen.width, Screen.height, 0);
return texture;
}
IEnumerator VideoPlay()
{
videoPlayer.targetTexture.Release();
videoPlayer.Prepare();
Debug.Log("准备播放");
yield return new WaitUntil(() => videoPlayer.isPrepared);
Debug.Log("准备完毕,开始播放");
videoPlayer.Play();
m_isShow = true;
StartCoroutine(updateVideoPlayTime());
yield return new WaitUntil(() => !videoPlayer.isPlaying);
Debug.Log("播放完毕");
}
/// <summary>
/// 当前视频节点
/// </summary>
int currentTime;
/// <summary>
/// 更新倒计时时间
/// </summary>
/// <returns></returns>
IEnumerator updateVideoPlayTime()
{
// 四舍五入取整
while (Mathf.RoundToInt((float)videoPlayer.time) <= Mathf.RoundToInt(videolength))
{
currentTime = Mathf.RoundToInt(videolength) - Mathf.RoundToInt((float)videoPlayer.time);
// 倒计时
countDownTimeTerxt.text = currentTime.ToString();
if (Mathf.RoundToInt((float)videoPlayer.time) >= Mathf.RoundToInt(videolength) &&
Mathf.RoundToInt((float)videoPlayer.time) != 0)
{
Debug.Log("看完视频");
break;
}
yield return new WaitForEndOfFrame();
}
}
/// <summary>
/// 视频总时长
/// </summary>
float videolength;
/// <summary>
/// 是否拿到视频总时长
/// </summary>
bool m_isShow;
private void Update()
{
if (videoPlayer.isPlaying && m_isShow)
{
videolength = videoPlayer.frameCount / videoPlayer.frameRate; // 总帧数/帧率
currentTime = Mathf.RoundToInt((float)videoPlayer.time);
m_isShow = !m_isShow;
}
}
private void OnDisable()
{
videoPlayer.Stop();
// 释放renderTexture
RenderTexture.ReleaseTemporary(texture);
videoPlayer.targetTexture = null;
}
}