unity制作简易播放器
先上效果图
我采用的解决方案是unity内置的video player,通过UGUI-rawimage进行显示,当然也可以使用quad方式,会更简单点。
1.界面搭建
- 首先需要在Texture文件夹*(方便管理)*中右键-新建 RenderTexture
- 然后新建一个rawimage,添加video player组件,添加Audio source组件
- 将新建的RenderTexture赋值给video player-target texture(这一步非常重要,否则不显示画面)和rawimage的texture(也可以通过代码添加)
- 将source设置为url,audio outputmode设置为Audio source(此处是为了解析视频中的声音,并赋给Audio source,否则播放的视频无声)
- UI搭建基本完成,然后就是控制播放了
2.时间显示
// 当前视频的总时间值和当前播放时间值的参数
private int currentHour;
private int currentMinute;
private int currentSecond;
private int clipHour;
private int clipMinute;
private int clipSecond;
//总时间显示
clipHour = (int)videoPlayer.clip.length / 3600;
clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;
clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);
longTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", clipHour, clipMinute, clipSecond);
/// <summary>
/// 视频时间显示,每帧调用
/// </summary>
void TimeShow()
{
// 当前的视频播放时间,
currentHour = (int)videoPlayer.time / 3600;
currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);
nowTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", currentHour, currentMinute, currentSecond);
}
3.播放暂停控制
初始设置,playOnAwak设置为false,并检测视频准备情况
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
videoPlayer.Prepare();
videoPlayer.Play();
这个简单加个bool值判断,调用默认播放和暂停函数就OK了,音频是视频解析出来的,所以控制了视频也就控制了音频。
videoPlayer.Play();
videoPlayer.Pause();
4.音量控制
音量控制就是修改Audio source的value了,通过slider滑动条控制,
sliderAudio.value = audioSource.volume;
sliderAudio.gameObject.SetActive(false);
sliderAudio.onValueChanged.AddListener((float value) => SliderChanger(value));
void SliderChanger(float value)
{
audioSource.volume = value;
}
5.进度条控制及拖拽
视频进度控制有两种形式:一种是按帧控制,一种按时间控制,两种都可以,做好换算就行了
sliderVideo.onValueChanged.AddListener((float value) => SliderEvent(value));
按帧控制
void Update()
{
if (videoPlayer.time % 1 == 0)//为了防止卡顿
{
int a = (int)videoPlayer.frame;
int b = (int)videoPlayer.frameCount;
sliderVideo.value =(float ) a / b;
TimeShow();//已播放时间
}
}
public void SliderEvent(float value)
{
videoPlayer.frame = long.Parse((value * videoPlayer.frameCount).ToString("0."));
TimeShow(); //更新时间
videoPlayer.targetTexture.Release();//清理无效纹理
}
按时间控制
sliderVideo.value=videoPlayer.time/videoPlayer.clip.length;
videoPlayer.time = value * videoPlayer.clip.length;
6.url相关控制
使用url可以播放本地视频(绝对地址),也可以播放网络视频
string url = Application.dataPath + "/StreamingAssets/***.mp4";
if (Directory.Exists(url))
{//此处是为验证地址的准确性,地址无效则播放预备视频
videoPlayer.url = url;
}
else
{
videoPlayer.clip = videoClip;
}
好了基本就这样,记录一下
/// <summary>
/// 视频播放控制
/// </summary>
public class VideoPlayerControl : MonoBehaviour
{
public Button buttonPlay, buttonSuspend, buttonAudio, buttonQuit;
public Slider sliderVideo, sliderAudio;
public Text nowTimeText, longTimeText;
public VideoPlayer videoPlayer;
public AudioSource audioSource;
bool isPlay, isAudio;
public VideoClip videoClip;//当url出错时,播放此视频
// 当前视频的总时间值和当前播放时间值的参数
private int currentHour;
private int currentMinute;
private int currentSecond;
private int clipHour;
private int clipMinute;
private int clipSecond;
void Start()
{
string url = Application.dataPath + "/StreamingAssets/1.mp4";
if (Directory.Exists(url))
{//此处是为验证地址的准确性,地址无效则播放预备视频
videoPlayer.url = url;
}
else
{
videoPlayer.clip = videoClip;
}
clipHour = (int)videoPlayer.clip.length / 3600;
clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;
clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);
longTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", clipHour, clipMinute, clipSecond);
buttonAudio.onClick.AddListener(AudioControl);
sliderAudio.value = audioSource.volume;
sliderAudio.gameObject.SetActive(false);
sliderAudio.onValueChanged.AddListener((float value) => SliderChanger(value));
buttonQuit.onClick.AddListener(UIClose);
}
private void OnEnable()
{
//videoPlayer.playOnAwake = false;
//audioSource.playOnAwake = false;
videoPlayer.Prepare();
videoPlayer.Play();
isPlay = true;
sliderVideo.onValueChanged.AddListener((float value) => SliderEvent(value));
buttonPlay.onClick.AddListener(VideoPlay);
buttonSuspend.onClick.AddListener(VideoPlay);
}
void Update()
{
if (videoPlayer.time % 1 == 0)//为了防止卡顿
{
int a = (int)videoPlayer.frame;
int b = (int)videoPlayer.frameCount;
sliderVideo.value =(float ) a / b;
TimeShow();//已播放时间
}
}
void VideoPlay()
{
isPlay = !isPlay;
if (isPlay)
{
videoPlayer.Play();
buttonSuspend.gameObject.SetActive(false);
buttonPlay.gameObject.SetActive(true);
}
else
{
videoPlayer.Pause();
buttonSuspend.gameObject.SetActive(true);
buttonPlay.gameObject.SetActive(false);
}
}
void AudioControl()
{
isAudio = !isAudio;
sliderAudio.gameObject.SetActive(isAudio);
}
void SliderChanger(float value)
{
audioSource.volume = value;
}
/// <summary>
/// 关闭
/// </summary>
void UIClose()
{
gameObject.SetActive(false);
}
public void SliderEvent(float value)
{
StartCoroutine(ChangerValue(value));
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
IEnumerator ChangerValue(float value)
{
videoPlayer.frame = long.Parse((value * videoPlayer.frameCount).ToString("0."));
TimeShow();//更新时间
yield return new WaitForSeconds(0.1f);
videoPlayer.targetTexture.Release();//清理无效纹理
}
/// <summary>
/// 视频时间显示
/// </summary>
void TimeShow()
{
// 当前的视频播放时间
currentHour = (int)videoPlayer.time / 3600;
currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);
nowTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", currentHour, currentMinute, currentSecond);
}
}