change
This commit is contained in:
@@ -1,130 +1,188 @@
|
||||
// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using FairyGUI;
|
||||
using NBC;
|
||||
using RenderHeads.Media.AVProVideo;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public enum VideoPlayMode
|
||||
{
|
||||
Top,
|
||||
Mid,
|
||||
Bottom
|
||||
}
|
||||
|
||||
public partial class VideoPlayer : GComponent
|
||||
{
|
||||
private VideoPlayerComponent _playerComponent;
|
||||
|
||||
private NTexture _texture;
|
||||
private float _defY;
|
||||
public bool IsLoading => stageCtrl.selectedIndex == 0;
|
||||
|
||||
private VideoRetData _videoRetData;
|
||||
public int VideoIndex { get; private set; }
|
||||
|
||||
private void OnInited()
|
||||
{
|
||||
VideoManager.Instance.OnPlayStart += OnPlayStart;
|
||||
VideoManager.Instance.OnHandleEvent += OnHandleEvent;
|
||||
BtnPause.onClick.Set(OnClickPause);
|
||||
}
|
||||
|
||||
SwipeGesture gesture = new SwipeGesture(BtnPause);
|
||||
gesture.onBegin.Set(OnSwipeBegin);
|
||||
gesture.onMove.Set(OnSwipeMove);
|
||||
gesture.onEnd.Set(OnSwipeEnd);
|
||||
gesture.onAction.Add(OnSwipeGesture);
|
||||
_defY = VideoVer.y;
|
||||
// Video.texture = new NTexture(Game.Instance.RT);
|
||||
// Player.on
|
||||
// Player.OpenMedia()
|
||||
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()
|
||||
{
|
||||
VideoManager.Instance.OnPlayStart -= OnPlayStart;
|
||||
VideoManager.Instance.OnHandleEvent -= OnHandleEvent;
|
||||
if (_texture != null)
|
||||
{
|
||||
_texture.Dispose();
|
||||
}
|
||||
StopVideo();
|
||||
// if (_texture != null)
|
||||
// {
|
||||
// _texture.Dispose();
|
||||
// }
|
||||
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
#region 手势
|
||||
|
||||
private float _offsetValue;
|
||||
|
||||
private void OnSwipeBegin(EventContext context)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnSwipeMove(EventContext context)
|
||||
{
|
||||
var gesture = context.sender as SwipeGesture;
|
||||
if (gesture == null) return;
|
||||
Log.Info($"gesture.delta.y={gesture.delta.y}");
|
||||
_offsetValue += gesture.delta.y;
|
||||
VideoVer.y = _defY + _offsetValue;
|
||||
}
|
||||
|
||||
private void OnSwipeEnd(EventContext context)
|
||||
{
|
||||
_offsetValue = 0;
|
||||
VideoVer.y = _defY;
|
||||
}
|
||||
|
||||
private void OnSwipeGesture(EventContext context)
|
||||
{
|
||||
SwipeGesture swipeGesture = (SwipeGesture)context.sender;
|
||||
var value = Math.Abs(swipeGesture.position.y);
|
||||
if (value < 200)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (swipeGesture.position.y < 0) //向上滑动
|
||||
{
|
||||
Log.Info($"向上滑动 y={swipeGesture.position.y}");
|
||||
VideoManager.Instance.Next();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Info($"向下滑动 y={swipeGesture.position.y}");
|
||||
VideoManager.Instance.Previous();
|
||||
}
|
||||
|
||||
VideoVer.alpha = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#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;
|
||||
pauseCtrl.selectedIndex = 1;
|
||||
}
|
||||
else if (eventType == MediaPlayerEvent.EventType.Unpaused)
|
||||
{
|
||||
pauseCtrl.selectedIndex = 0;
|
||||
}
|
||||
// else if (eventType == MediaPlayerEvent.EventType.Unpaused ||
|
||||
// eventType == MediaPlayerEvent.EventType.Started)
|
||||
// {
|
||||
// // 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();
|
||||
}
|
||||
|
||||
_texture = new NTexture(rt);
|
||||
VideoVer.texture = _texture;
|
||||
if (_playerComponent != null)
|
||||
{
|
||||
_texture = new NTexture(_playerComponent.NowRT)
|
||||
{
|
||||
destroyMethod = DestroyMethod.Custom
|
||||
};
|
||||
VideoVer.texture = _texture;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 播放控制
|
||||
|
||||
private void OnClickPause()
|
||||
{
|
||||
VideoManager.Instance.TogglePlayPause();
|
||||
_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
|
||||
|
||||
Reference in New Issue
Block a user