i have this code that plays a video from a link provided. now i want to create a button that on click it will change the link of the video player; and play the other link provided in the button actions.
this is how i play the video:
IEnumerator playVideo()
{
videoPlayer = gameObject.AddComponent<VideoPlayer>();
audioSource = gameObject.AddComponent<AudioSource>();
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
audioSource.Pause();
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.funnymasti.com/data/21955.html";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
Debug.Log("Done Preparing Video");
try
{
image.texture = videoPlayer.texture;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
videoPlayer.Play();
audioSource.Play();
}
and the button event on click: (i need help here)
public void Button_Click()
{
stop current video playing
change video url ( videoPlayer.url = another link)
refresh video player to show new video
play new video
}
thanks for the help
One cannot just change the video like that, but you will have to call a coroutine again. Read about coroutines. You have all the code there, just need to bake it:
I can tell you the process:
- On the button click you will have to trigger a coroutine which is a smiliar function as the Ienumerator you have written
- Same code goes in the Ienumerator except the URL
Maybe you can be a little smart by passing the URL in the ienumerator.
Cheers!
1 Like
thanks for the help @iamvideep_1 . i tried what you told me. the video plays well when i hit the button but the image stays the previous video going on. so i have the second video’s sound and the first video image
public void Button_Click()
{
StartCoroutine(playVideo2());
}
void Start()
{
Application.runInBackground = true;
StartCoroutine(playVideo());
}
IEnumerator playVideo()
{
videoPlayer = gameObject.AddComponent<VideoPlayer>();
audioSource = gameObject.AddComponent<AudioSource>();
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
audioSource.Pause();
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
Debug.Log("Done Preparing Video");
try
{
image.texture = videoPlayer.texture;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
videoPlayer.Play();
audioSource.Play();
}
IEnumerator playVideo2()
{
videoPlayer = gameObject.AddComponent<VideoPlayer>();
audioSource = gameObject.AddComponent<AudioSource>();
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
audioSource.Pause();
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.funnymasti.com/data/21955.html";
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
Debug.Log("Done Preparing Video");
try
{
image.texture = videoPlayer.texture;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
videoPlayer.Play();
audioSource.Play();
}
Just a guess, but you probably need to stop the first coroutine before kicking off the second.
The StartCoroutine() call returns a Coroutine object, and then you can use it to StopCoroutine() before starting the other one.
Oop, belay that… I see that the actual Play call is the final thing in the coroutine, so my suggestion is probably not helpful.