190 lines
5.1 KiB
C#
190 lines
5.1 KiB
C#
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
|
|
|
using UnityEngine;
|
|
using FairyGUI;
|
|
using RenderHeads.Media.AVProVideo;
|
|
|
|
namespace NBF
|
|
{
|
|
public enum VideoPlayMode
|
|
{
|
|
Top,
|
|
Mid,
|
|
Bottom
|
|
}
|
|
|
|
public partial class VideoPlayer : GComponent
|
|
{
|
|
private VideoPlayerComponent _playerComponent;
|
|
|
|
private NTexture _texture;
|
|
public bool IsLoading => stageCtrl.selectedIndex == 0;
|
|
|
|
private VideoRetData _videoRetData;
|
|
public int VideoIndex { get; private set; }
|
|
|
|
private void OnInited()
|
|
{
|
|
BtnPause.onClick.Set(OnClickPause);
|
|
}
|
|
|
|
public void PlayVideo(int videoIndex, VideoPlayMode mode)
|
|
{
|
|
VideoIndex = videoIndex;
|
|
name = videoIndex.ToString();
|
|
gameObjectName = videoIndex.ToString();
|
|
if (VideoIndex < 0) return;
|
|
_videoRetData = Game.Instance.GetVideoDataByIndex(videoIndex);
|
|
if (_playerComponent != null)
|
|
{
|
|
StopVideo();
|
|
}
|
|
|
|
// stageCtrl.selectedIndex = 0;
|
|
// _videoRetData = videoRetData;
|
|
_playerComponent = VideoManager.Instance.GetPlayer(VideoIndex);
|
|
_playerComponent.VideoIndex = videoIndex;
|
|
_playerComponent.OnPlayStart += OnPlayStart;
|
|
_playerComponent.OnHandleEvent += OnHandleEvent;
|
|
var videoUrl = $"{Net.Instance.ServerUrl}/files/videos/{_videoRetData.FilePath}";
|
|
|
|
_playerComponent.ChangeVideo(videoUrl, mode == VideoPlayMode.Mid);
|
|
SetRt();
|
|
// _playerComponent =
|
|
}
|
|
|
|
public void StopVideo()
|
|
{
|
|
// var offset = _playerComponent.VideoIndex
|
|
// if(_playerComponent.VideoIndex)
|
|
_playerComponent.OnPlayStart -= OnPlayStart;
|
|
_playerComponent.OnHandleEvent -= OnHandleEvent;
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
base.OnUpdate();
|
|
UpdateTick();
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
StopVideo();
|
|
// if (_texture != null)
|
|
// {
|
|
// _texture.Dispose();
|
|
// }
|
|
|
|
base.Dispose();
|
|
}
|
|
|
|
|
|
#region 视频播放回调
|
|
|
|
private void OnHandleEvent(MediaPlayerEvent.EventType eventType)
|
|
{
|
|
if (isDisposed) return;
|
|
if (displayObject == null) return;
|
|
if (displayObject.gameObject == null) return;
|
|
if (eventType == MediaPlayerEvent.EventType.Paused)
|
|
{
|
|
pauseCtrl.selectedIndex = 1;
|
|
}
|
|
else if (eventType == MediaPlayerEvent.EventType.Unpaused)
|
|
{
|
|
pauseCtrl.selectedIndex = 0;
|
|
}
|
|
else if (eventType == MediaPlayerEvent.EventType.Started)
|
|
{
|
|
_watchCount = 1;
|
|
pauseCtrl.selectedIndex = 0;
|
|
stageCtrl.selectedIndex = 1;
|
|
StartedAnim.Play();
|
|
}
|
|
else if (eventType == MediaPlayerEvent.EventType.StartedSeeking)
|
|
{
|
|
_watchCount++;
|
|
Tick(); //重播时也上报tick
|
|
}
|
|
}
|
|
|
|
private void OnPlayStart(RenderTexture rt)
|
|
{
|
|
// if (_texture != null)
|
|
// {
|
|
// _texture.Dispose();
|
|
// }
|
|
//
|
|
// if (_playerComponent != null)
|
|
// {
|
|
// _texture = new NTexture(_playerComponent.NowRT);
|
|
// VideoVer.texture = _texture;
|
|
// }
|
|
|
|
if (_texture.width != rt.width || _texture.height != rt.height)
|
|
{
|
|
SetRt();
|
|
}
|
|
|
|
// _texture.width = rt.width;
|
|
// _texture.
|
|
stageCtrl.selectedIndex = 1;
|
|
}
|
|
|
|
private void SetRt()
|
|
{
|
|
if (_texture != null)
|
|
{
|
|
_texture.Dispose();
|
|
}
|
|
|
|
if (_playerComponent != null)
|
|
{
|
|
_texture = new NTexture(_playerComponent.NowRT)
|
|
{
|
|
destroyMethod = DestroyMethod.Custom
|
|
};
|
|
VideoVer.texture = _texture;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 播放控制
|
|
|
|
private void OnClickPause()
|
|
{
|
|
_playerComponent.TogglePlayPause();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 视频播放tick
|
|
|
|
private float _videoPlayTime;
|
|
private int _watchCount;
|
|
|
|
private void UpdateTick()
|
|
{
|
|
if (IsLoading) return;
|
|
if (_playerComponent.IsPlaying)
|
|
{
|
|
_videoPlayTime += Time.deltaTime;
|
|
if (_videoPlayTime >= 5)
|
|
{
|
|
Tick(); //5秒上报一次
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Tick()
|
|
{
|
|
if (_videoRetData == null) return;
|
|
Net.Instance.Tick(_videoRetData.Id, (int)_videoPlayTime, _watchCount);
|
|
_watchCount = 0;
|
|
_videoPlayTime = 0;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |