当前位置:网站首页>Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
2022-07-31 13:40:00 【Lawa0592】
1. Seeking功能的相关接口
通过双精度时间寻找跳转:
- Seek() ⇒ 跳转到精确的指定时间上的画面
- SeekFast() ⇒ 跳转到指定时间上最近的帧画面
- SeekWithTolerance() ⇒ 跳转到指定时间上某个范围时间内的画面(只支持macOS,ios,tvOS)
通过使用帧寻找跳转(只对具有已知恒定帧速率的媒体有效):
- SeekToFrame() ⇒ 跳转到具体某一帧的画面
- SeekToFrameRelative() ⇒ 跳转到相对于当前帧向前或向后多少帧的画面
底层接口源码如下:
/// <summary>
/// The time in seconds seeked will be to the exact time
/// This can take a long time is the keyframes are far apart
/// Some platforms don't support this and instead seek to the closest keyframe
/// </summary>
void Seek(double time);
/// <summary>
/// The time in seconds seeked will be to the closest keyframe
/// </summary>
void SeekFast(double time);
/// <summary>
/// The time in seconds seeked to will be within the range [time-timeDeltaBefore, time+timeDeltaAfter] for efficiency.
/// Only supported on macOS, iOS and tvOS.
/// Other platforms will automatically pass through to Seek()
/// </summary>
void SeekWithTolerance(double time, double timeDeltaBefore, double timeDeltaAfter);
/// <summary>
/// Seek to a specific frame, range is [0, GetMaxFrameNumber()]
/// NOTE: For best results the video should be encoded as keyframes only
/// and have no audio track, or an audio track with the same length as the video track
/// </summary>
void SeekToFrame(int frame, float overrideFrameRate = 0f);
/// <summary>
/// Seek forwards or backwards relative to the current frame
/// NOTE: For best results the video should be encoded as keyframes only
/// and have no audio track, or an audio track with the same length as the video track
/// </summary>
void SeekToFrameRelative(int frameOffset, float overrideFrameRate = 0f);
需要注意的是,跳转的响应/行为在不同平台会有不同差异
| 平台 | 快速近似关键帧搜索(Fast Approximate Keyframe Seeking) | 慢速精确寻帧(Slow Accurate Frame Seeking) |
|---|---|---|
| Windows (WinRT / Media Foundation) | * | * |
| Windows (DirectShow) | * | 取决于编解码器 |
| Android (ExoPlayer) | * | * |
| Android (MediaPlayer) | * | API 26 及以上 |
| macOS | * | * |
| iOS/iPadOS/tvOS | * | * |
| WebGL | * | 视情况而变化 |
2. 跳转(Seeking)功能的实现案例
具体的实现可以参考AVPro提供的Demo(Demo_MediaPlayer场景)
下面只列出结合时间条(slider)进行视频跳转的部分:
[Header("UI Components")]
[SerializeField] Slider _sliderTime = null;
void Start()
{
CreateTimelineDragEvents();
}
private void CreateTimelineDragEvents()
{
EventTrigger trigger = _sliderTime.gameObject.GetComponent<EventTrigger>();
if (trigger != null)
{
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener((data) => {
OnTimeSliderBeginDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener((data) => {
OnTimeSliderDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener((data) => {
OnTimeSliderEndDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((data) => {
OnTimelineBeginHover((PointerEventData)data); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerExit;
entry.callback.AddListener((data) => {
OnTimelineEndHover((PointerEventData)data); });
trigger.triggers.Add(entry);
}
}
private bool _wasPlayingBeforeTimelineDrag;
private void OnTimeSliderBeginDrag()
{
if (_mediaPlayer && _mediaPlayer.Control != null)
{
_wasPlayingBeforeTimelineDrag = _mediaPlayer.Control.IsPlaying();
if (_wasPlayingBeforeTimelineDrag)
{
_mediaPlayer.Pause();
}
OnTimeSliderDrag();
}
}
private void OnTimeSliderDrag()
{
if (_mediaPlayer && _mediaPlayer.Control != null)
{
TimeRange timelineRange = GetTimelineRange();
double time = timelineRange.startTime + (_sliderTime.value * timelineRange.duration);
_mediaPlayer.Control.Seek(time);
_isHoveringOverTimeline = true;
}
}
private void OnTimeSliderEndDrag()
{
if (_mediaPlayer && _mediaPlayer.Control != null)
{
if (_wasPlayingBeforeTimelineDrag)
{
_mediaPlayer.Play();
_wasPlayingBeforeTimelineDrag = false;
}
}
}
private bool _isHoveringOverTimeline;
private void OnTimelineBeginHover(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject != null)
{
_isHoveringOverTimeline = true;
_sliderTime.transform.localScale = new Vector3(1f, 2.5f, 1f);
}
}
private void OnTimelineEndHover(PointerEventData eventData)
{
_isHoveringOverTimeline = false;
_sliderTime.transform.localScale = new Vector3(1f, 1f, 1f);
}
参考文献
边栏推荐
猜你喜欢

浏览器被hao360劫持解决办法

Reasons and solutions for Invalid bound statement (not found)

How to quickly split and merge cell data in Excel

C#高级--委托

网络协议及相关技术详解

Edge Cloud Explained in Simple Depth | 4. Lifecycle Management

报错IDEA Terminated with exit code 1

使用CompletableFuture进行异步处理业务

技能大赛训练题:MS15_034漏洞验证与安全加固

关于MySQL主从复制的数据同步延迟问题
随机推荐
代码随想录笔记_哈希_454四数相加II
selenium被反爬了怎么办?
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
AI cocoa AI frontier introduction (7.31)
C# control StatusStrip use
Open Inventor 10.12 重大改进--和谐版
VU 非父子组件通信
六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
ECCV2022: Recursion on Transformer without adding parameters and less computation!
DELL SC compellent 康贝存储系统怎么抓取配置信息
Sliding window method to segment data
365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
生产力工具和插件
sqlalchemy determines whether a field of type array has at least one consistent data with an array
Usage of += in C#
基于模糊预测与扩展卡尔曼滤波的野值剔除方法
技能大赛训练题:登录安全加固
Productivity Tools and Plugins
go使用makefile脚本编译应用
浏览器被hao360劫持解决办法