0
\$\begingroup\$

I'm trying to play a video when a text command is typed.

What I expect to happen is the user inputs "play video 1" in an input text box, and after the command is sent via a button press, the video will play.

To remove the video, the user then types "clear video" in the input text box. After the command is sent via a button press, the video will stop playing and not be visible any longer until the user again types "play video 1" into the input text box.

this is the code I have so far but I can't get it to work.

{
//this is where it will play the video. using a raw image with a render texture  
    public VideoPlayer videoPlayer  
  
//this will be the placeholder when no video is playing
    public Texture nVideo;

// these are the videoclips
    public videoclip m_video1;
    public videoclip m_video2;
    public videoclip m_video3;
//

    private string lastInput;



    void Start()
    {
        videoPlayer = GetComponent<VideoPlayer>();
    }

    void Update()
    {
       
        if  (lastInput == "clear video")
            {
            Texture.Texture = nVideo;
           }
        else if
           (lastInput == "show video 1")
            {
            playvideo = m_video1;
        }
        else if
          (lastInput == "show video 2")
        {
             playvideo = m_video2;
        }
        else if
           (lastInput == "show video 3")
            {
             playvideo = m_video3;
        }
        }

 public void GetInputText(InputField input)
    {

        //lastInput = input.text.ToLower();
        lastInput = input.text.ToString();
    }
    

}

As you can see, the problem I am having is I don't have the correct namespaces and I'm also sure I don't have the right commands to play the video.

I hope someone understands what I'm trying to accomplish and can point me in the right direction. Any help will be greatly appreciated.

\$\endgroup\$
6
  • \$\begingroup\$ Hello, yes is a monobehaviour. What I mean with correct name spaces is like what your next question asks playvideo is just a placeholder ((actually most of the code is just a placeholder)) cause I don't know how to call the function correctly. yes after the user "inputs "play video 1" then presses a button to send the the message to the script. the video should play. the original script I ma using is to show images I just want to change it to show videos. \$\endgroup\$ Jun 15, 2023 at 6:14
  • \$\begingroup\$ Have you first tried to simplify your code to just play the video on a button click? To start the video, all that is needed should be videoPlayer.Play(); \$\endgroup\$
    – Zibelas
    Jun 15, 2023 at 6:18
  • \$\begingroup\$ Something like this? else if (lastInput == "show video 1") { videoPlayer.Play(); but how would it know its video 1 \$\endgroup\$ Jun 15, 2023 at 6:20
  • \$\begingroup\$ You don't need to go the route of checking against certain input. Make a new method called PlayingCurrentSetVideo or however you want to name it, inside call the videoPlayer.Play(); and call that method from a Test Play video button in your scene. Once that one is working and you can see your video, the refinement would be switching videos with your input string. If simple is already failing, complex is just added extra failure points that are hard to find \$\endgroup\$
    – Zibelas
    Jun 15, 2023 at 6:22
  • \$\begingroup\$ Thank you I will try that and come back and tell you how it went. \$\endgroup\$ Jun 15, 2023 at 6:24

1 Answer 1

-2
\$\begingroup\$

Here's an updated version of your code

using UnityEngine; using UnityEngine.UI; using UnityEngine.Video;

public class VideoPlayerController : MonoBehaviour { public VideoPlayer videoPlayer; public RawImage videoImage; public Texture noVideoTexture;

public VideoClip videoClip1;
public VideoClip videoClip2;
public VideoClip videoClip3;

private string lastInput;

private void Start()
{
    videoPlayer = GetComponent<VideoPlayer>();
}

private void Update()
{
    if (lastInput == "clear video")
    {
        videoPlayer.Stop();
        videoImage.texture = noVideoTexture;
    }
    else if (lastInput == "play video 1")
    {
        PlayVideo(videoClip1);
    }
    else if (lastInput == "play video 2")
    {
        PlayVideo(videoClip2);
    }
    else if (lastInput == "play video 3")
    {
        PlayVideo(videoClip3);
    }
}

private void PlayVideo(VideoClip videoClip)
{
    videoPlayer.Stop();
    videoPlayer.clip = videoClip;
    videoPlayer.Play();
    videoImage.texture = videoPlayer.texture;
}

public void GetInputText(InputField input)
{
    lastInput = input.text.ToLower();
    input.text = ""; // Clear the input field after processing the command
}

}

\$\endgroup\$
4
  • 5
    \$\begingroup\$ Did you test that code? Just from reading it I would say it should never play the video longer than an Update cycle before stopping and starting it again \$\endgroup\$
    – Zibelas
    Jun 16, 2023 at 16:44
  • \$\begingroup\$ Can you tell us what the mistakes in the question are, how you fixed them and why those fixes work better? \$\endgroup\$
    – Philipp
    Jun 17, 2023 at 17:48
  • \$\begingroup\$ Hello, sorry I took so long to respond. I tried your code and it gives no errors but nothing happens when I type "play video 1" or any of the other commands. I am still trying to figure out why I pretty much have the same thing but was trying to use the set active command instead of the play video but I have not figured out how to get it to work. \$\endgroup\$ Jun 23, 2023 at 4:01
  • \$\begingroup\$ @LaBrownBag this answer is not really playing the video since it restarts the video every Update cycle. It might work if you enter a valid input and afterwards enter an invalid input (but still is not great of an answer in itself) \$\endgroup\$
    – Zibelas
    Jun 26, 2023 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .