0

I am trying to create multiple buttons with Unity 3d and add each of them on click listener, when the user clicks it should open the given link to the video player , I am beginner in unity and please help me I tried this one

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;


/// <summary>
/// Unity VideoPlayer Script for Unity 5.6 (currently in beta 0b11 as of March 15, 2017)
/// Blog URL: http://justcode.me/unity2d/how-to-play-videos-on-unity-using-new-videoplayer/
/// YouTube Video Link: https://www.youtube.com/watch?v=nGA3jMBDjHk
/// StackOverflow Disscussion: http://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video/
/// Code Contiburation: StackOverflow - Programmer
/// </summary>


public class StreamVideo : MonoBehaviour {

    public RawImage image;
    public GameObject playIcon;

    public VideoClip videoToPlay;

    private VideoPlayer videoPlayer;
    private VideoSource videoSource;

    private AudioSource audioSource;

    private bool isPaused = false;
    private bool firstRun = true;

    IEnumerator playVideo() {
        playIcon.SetActive(false);
        firstRun = false;
        //Add VideoPlayer to the GameObject
        videoPlayer = gameObject.AddComponent<VideoPlayer>();

        //Add AudioSource
        audioSource = gameObject.AddComponent<AudioSource>();

        //Disable Play on Awake for both Video and Audio
        videoPlayer.playOnAwake = false;
        audioSource.playOnAwake = false;
        audioSource.Pause();

        //We want to play from video clip not from url

        videoPlayer.source = VideoSource.VideoClip;

        // Vide clip from Url
        //videoPlayer.source = VideoSource.Url;
        //videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";


        //Set Audio Output to AudioSource
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

        //Assign the Audio from Video to AudioSource to be played
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);

        //Set video To Play then prepare Audio to prevent Buffering
        videoPlayer.clip = videoToPlay;
        videoPlayer.Prepare();

        //Wait until video is prepared
        while (!videoPlayer.isPrepared) {
            yield return null;
        }

        Debug.Log("Done Preparing Video");

        //Assign the Texture from Video to RawImage to be displayed
        image.texture = videoPlayer.texture;

        //Play Video
        videoPlayer.Play();

        //Play Sound
        audioSource.Play();

        Debug.Log("Playing Video");
        while (videoPlayer.isPlaying) {
            Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
            yield return null;
        }

        Debug.Log("Done Playing Video");
    }

    public void PlayPause() {
        if(!firstRun && !isPaused) {
            videoPlayer.Pause();
            audioSource.Pause();
            playIcon.SetActive(true);
            isPaused = true;
        } else if (!firstRun && isPaused) {
            videoPlayer.Play();
            audioSource.Play();
            playIcon.SetActive(false);
            isPaused = false;
        } else {
            StartCoroutine(playVideo());
        }
    }
}

but it only plays one video ,I don't know how to create buttons and how to add on click event listener that should play the selected video on the video view in landscape mode,can someone give me some example with one button, how to interact the buttons with the video player , I will appreciate your help

andrew
  • 83
  • 1
  • 14
  • What do you mean by "open the given link"? Where are the video links stored? Please read the link of the code you provided in your answer. It says you must **remove** `public VideoClip videoToPlay;` and `videoPlayer.clip = videoToPlay;`. You have not done these yet. Read the section that says **Play Video From URL:** – Programmer Feb 18 '18 at 00:37
  • I want to add links to each of buttons on button click it should open the link to video player – andrew Feb 18 '18 at 09:08
  • @Programmer I need help regarding all the thing that I mentioned above – andrew Feb 18 '18 at 10:26
  • [This](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130?s=1|0.0000#41392130) is how to detect button clicks. Use that to accomplish this. If you still have problems, edit your code with your new code that includes the button detection part. Again It says you must **remove** `public VideoClip videoToPlay;` and `videoPlayer.clip = videoToPlay;` to play video from a url. You have not done these yet. – Programmer Feb 18 '18 at 11:09
  • any example how to make each button open diferent url – andrew Feb 18 '18 at 15:02
  • 1
    The link I provided shows how to detect multiple button clicks in **#2**. You can add your different url for each button there. Just show that you've put effort into actually doing this – Programmer Feb 18 '18 at 15:06
  • thank you very much, I will mark your answer correct but I am new to this and I don't know how to connect things,where to create buttons , how to add links and then play on video player, any example with one button will be appreciated, I will mark your question correct anyway – andrew Feb 18 '18 at 15:23
  • You can't mark anything because I did not leave answer yet. what I've been doing is leaving comments. You cannot mark my answer I left on another question that is not yours. I am not helping you if I just give you a code. [Here](https://unity3d.com/learn/tutorials/s/user-interface-ui) is a UI tutorial for Unity. It shows how to create UI buttons and others. Once you learn that, understand basic C# stuff then comeback and see if you can at least combine creating UI, detecting clicks and playing video together – Programmer Feb 18 '18 at 15:28
  • ok thank you , I didn't want you to do things for me, I just wanted an example how it works, thanks a lot – andrew Feb 18 '18 at 15:31

0 Answers0