first commit
This commit is contained in:
13
Assets/Scripts/Common/Attributes.cs
Normal file
13
Assets/Scripts/Common/Attributes.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public abstract class BaseAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UIExtensionAutoBindAttribute : BaseAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Common/Attributes.cs.meta
Normal file
3
Assets/Scripts/Common/Attributes.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f1bfbffdc2946b7911b0fcedc1d4c3d
|
||||
timeCreated: 1770453200
|
||||
192
Assets/Scripts/Common/VideoManager.cs
Normal file
192
Assets/Scripts/Common/VideoManager.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NBC;
|
||||
using RenderHeads.Media.AVProVideo;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class VideoManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private MediaPlayer _mediaPlayer;
|
||||
|
||||
public static VideoManager Instance { get; private set; }
|
||||
public ResolveToRenderTexture ResolveToRenderTexture;
|
||||
|
||||
public RenderTexture NowRT;
|
||||
|
||||
public Action<RenderTexture> OnPlayStart;
|
||||
public Action<MediaPlayerEvent.EventType> OnHandleEvent;
|
||||
|
||||
private int _nowPlayUrlIndex = 0;
|
||||
|
||||
public float PlayProgress { get; private set; } = 0f;
|
||||
|
||||
private List<string> _urls = new List<string>()
|
||||
{
|
||||
"http://10.10.0.158:9888/files/video/1.mp4",
|
||||
"http://10.10.0.158:9888/files/video/2.mp4",
|
||||
"http://10.10.0.158:9888/files/video/3.mp4",
|
||||
};
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
_mediaPlayer.Events.AddListener(HandleEvent);
|
||||
|
||||
// Start adaptive stream using the highest resolution - ExoPlayer only
|
||||
_mediaPlayer.PlatformOptionsAndroid.videoApi = Android.VideoApi.ExoPlayer;
|
||||
_mediaPlayer.PlatformOptionsAndroid.startWithHighestBitrate = true;
|
||||
|
||||
// Set the maximum adaptive resolution to 1080p - ExoPlayer only
|
||||
_mediaPlayer.PlatformOptionsAndroid.videoApi = Android.VideoApi.ExoPlayer;
|
||||
_mediaPlayer.PlatformOptionsAndroid.preferredMaximumResolution =
|
||||
MediaPlayer.OptionsAndroid.Resolution._1080p;
|
||||
|
||||
// // Set the peak adaptive bitrate to 4Mbps - ExoPlayer only
|
||||
// _mediaPlayer.PlatformOptionsAndroid.videoApi = Android.VideoApi.ExoPlayer;
|
||||
// _mediaPlayer.PlatformOptionsAndroid.preferredPeakBitRate = 4.0f;
|
||||
// _mediaPlayer.PlatformOptionsAndroid.preferredPeakBitRateUnits =
|
||||
// MediaPlayer.OptionsAndroid.BitRateUnits.Mbps;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_mediaPlayer) return;
|
||||
|
||||
if (_mediaPlayer.Info != null)
|
||||
{
|
||||
TimeRange timelineRange = GetTimelineRange();
|
||||
double t = 0.0;
|
||||
if (timelineRange.duration > 0.0)
|
||||
{
|
||||
t = ((_mediaPlayer.Control.GetCurrentTime() - timelineRange.startTime) / timelineRange.duration);
|
||||
}
|
||||
|
||||
PlayProgress = Mathf.Clamp01((float)t);
|
||||
}
|
||||
}
|
||||
|
||||
#region 事件
|
||||
|
||||
void HandleEvent(MediaPlayer mp, MediaPlayerEvent.EventType eventType, ErrorCode code)
|
||||
{
|
||||
Debug.Log("MediaPlayer " + mp.name + " generated event: " + eventType.ToString());
|
||||
OnHandleEvent?.Invoke(eventType);
|
||||
if (eventType == MediaPlayerEvent.EventType.Error)
|
||||
{
|
||||
Debug.LogError("Error: " + code);
|
||||
}
|
||||
else if (eventType == MediaPlayerEvent.EventType.MetaDataReady)
|
||||
{
|
||||
Log.Info("播放数据已准备====");
|
||||
}
|
||||
else if (eventType == MediaPlayerEvent.EventType.Started)
|
||||
{
|
||||
var width = mp.Info.GetVideoWidth();
|
||||
var height = mp.Info.GetVideoHeight();
|
||||
Log.Info("播放开始====");
|
||||
if (NowRT != null)
|
||||
{
|
||||
NowRT.Release();
|
||||
}
|
||||
|
||||
NowRT = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
|
||||
|
||||
ResolveToRenderTexture.ExternalTexture = NowRT;
|
||||
OnPlayStart?.Invoke(NowRT);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 播放控制
|
||||
|
||||
public void TogglePlayPause()
|
||||
{
|
||||
if (_mediaPlayer && _mediaPlayer.Control != null)
|
||||
{
|
||||
if (_mediaPlayer.Control.IsPlaying())
|
||||
{
|
||||
Pause();
|
||||
}
|
||||
else
|
||||
{
|
||||
Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
if (_mediaPlayer && _mediaPlayer.Control != null)
|
||||
{
|
||||
_mediaPlayer.Play();
|
||||
|
||||
#if UNITY_ANDROID || UNITY_OPENHARMONY
|
||||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private void Pause()
|
||||
{
|
||||
if (_mediaPlayer && _mediaPlayer.Control != null)
|
||||
{
|
||||
_mediaPlayer.Pause();
|
||||
#if UNITY_ANDROID || UNITY_OPENHARMONY
|
||||
Screen.sleepTimeout = SleepTimeout.SystemSetting;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private TimeRange GetTimelineRange()
|
||||
{
|
||||
if (_mediaPlayer.Info != null)
|
||||
{
|
||||
return Helper.GetTimelineRange(_mediaPlayer.Info.GetDuration(),
|
||||
_mediaPlayer.Control.GetSeekableTimes());
|
||||
}
|
||||
|
||||
return new TimeRange();
|
||||
}
|
||||
|
||||
public void Next()
|
||||
{
|
||||
if (_nowPlayUrlIndex >= _urls.Count - 1)
|
||||
{
|
||||
_nowPlayUrlIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_nowPlayUrlIndex++;
|
||||
}
|
||||
|
||||
ChangeVideo();
|
||||
}
|
||||
|
||||
public void Previous()
|
||||
{
|
||||
if (_nowPlayUrlIndex <= 0)
|
||||
{
|
||||
_nowPlayUrlIndex = _urls.Count - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
_nowPlayUrlIndex--;
|
||||
}
|
||||
|
||||
ChangeVideo();
|
||||
}
|
||||
|
||||
public void ChangeVideo()
|
||||
{
|
||||
_mediaPlayer.CloseMedia();
|
||||
_mediaPlayer.SetMediaPath(new MediaPath(_urls[_nowPlayUrlIndex], MediaPathType.AbsolutePathOrURL));
|
||||
_mediaPlayer.OpenMedia();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Common/VideoManager.cs.meta
Normal file
3
Assets/Scripts/Common/VideoManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb49b3d3fe4d41268789ab6546d8d824
|
||||
timeCreated: 1770457782
|
||||
Reference in New Issue
Block a user