Hello,
I’m trying to play a video from a url like this: mywebsite.com is available for purchase - Sedo.com…
I can play it and use a slider to move forward in the video.
However, I can’t go back. The playback time of the VideoPlayer remains unchanged.
I would like to know if this is a normal behavior because I am using a url or if it is a problem related to the VideoPlayer?
The VideoPlayer seems to have only a way to go forward in the playback (StepForward method and canStep property to know if we can go forward) which gives me the impression that we can’t go backward, is it really impossible?
I’m currently using Unity 2019.4.2f1, I’ve been looking for answers for several days but I haven’t found anything about the use of urls. If you have links to one or more topics that deal with the same problem, I’m interested in.
Finally, I ask my last question, is there a way to go backwards in the reading of a video by url?
I’m attaching the source code that I use to launch the video and to move forward/backward with the slider.
Have a nice day,
Antonin
Source code to load the video:
public class UIVideoPlayerContainer : UIVideoContainer
{
public VideoPlayer videoPlayer;
public AudioSource audioSource;
public GameObject videoWaiting;
public VideoNavBar videoPlayerBar;
float targetTime = 0;
private void Start()
{
videoPlayer.source = VideoSource.Url;
videoPlayer.prepareCompleted += delegate
{
videoWaiting.SetActive(false);
videoPlayerBar.Enabled = true;
videoPlayer.Play();
videoPlayer.time = targetTime;
};
}
public override IEnumerator PlayUrl(string url, float startTime = 0)
{
if (!url.Contains("https://") || url == "")
{
Debug.LogError("Bad video url:" + url);
yield break;
}
videoPlayerBar.Enabled = false;
videoWaiting.SetActive(true);
if (videoPlayer.isPlaying || videoPlayer.isPaused)
{
videoPlayer.Stop();
videoPlayer.url = "";
}
videoPlayer.url = url;
videoPlayer.SetTargetAudioSource(0, audioSource);
targetTime = startTime;
videoPlayer.Prepare();
}
public override IEnumerator Reset()
{
int step = 0;
while(step < 2)
{
if (step == 0)
videoPlayer.Stop();
else if (step == 1)
base.Reset();
step++;
yield return null;
}
}
}
Source code attached to the slider:
[Serializable]
public class NavigationSliderEvent : UnityEvent<float>
{ }
[RequireComponent(typeof(Slider))]
public class NavigationSlider : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public VideoPlayer videoPlayer;
public Slider slider;
public NavigationSliderEvent OnSlideFinished;
public bool slide = false;
private void Start()
{
slider = GetComponent<Slider>();
}
private void Update()
{
if(!slide)
slider.value = (float)videoPlayer.frame / videoPlayer.frameCount;
}
public void SetTime(float value)
{
videoPlayer.frame = (long)(value * videoPlayer.frameCount);
}
public void OnPointerDown(PointerEventData eventData)
{
slide = true;
videoPlayer.Pause();
}
public void OnPointerUp(PointerEventData eventData)
{
OnSlideFinished?.Invoke(slider.value);
slide = false;
}
}
I’ve tried with videoPlayer.time and videoPlayer.frame but neither of them want to go backwards.
SetTime is called using OnSlideFinished in the Unity editor.