Using VideoPlayer to play videos located on android device

Hello, I’m trying to use the new VideoPlayer class to play a video file on my android device, unfortunately I’m getting a NullReferenceException:

NullReferenceException: Object reference not set to an instance of an object
at VideoPlayerUI.SetVideoURL (System.String url) [0x00000] in :0

This is my code in VideoPlayerUI.cs

    public void SetVideoURL(string url)
    {
        _videoPlayer.source = VideoSource.Url;
        _videoPlayer.url = url;
    }

I made sure the url that is being passed is valid, it looks like this: “/storage/emulated/0/Android/data/com.company.project/files/myVideo.mp4”

I’ve also tried adding “file://” to the start of the url and still the same result.

I then tried to build the project with that same path already set on the videoplayer object, then it would work (although the video is choppy when it plays) but I still need to set the video url during runtime, which isn’t working.

when I try this code in the editor with a video file on my desktop it works and I don’t get the null reference exception.

I’m not sure what I’m missing here, any help is very appreciated, thanks!

One last question, is there a way to import videos into VideoClip assets during runtime?

Android device: Samsung S4
Unity version: 2017.1.0b9

2 Likes

I figured out what’s wrong, somehow when this function is called _videoPlayer becomes null, although it is being set on the Start function as GetComponent() … I fixed this by just doing a null check and if it’s null I re-initialize it

1 Like

@Sm0keh Hello, I’m not sure if you have any insights about this, but I am exporting from Unity an Android project so that I can open it in Android Studio. This project uses the camera (it’s an AR project), and right after I open it in Android Studio, I have a word in the Manifest that is unresolved, that word being: “VideoPlayer”. Here is the line:
<activity android:name=“com.unity3d.player.VideoPlayer” android:label=" @anon_76954444 /app_name" android:(…)>

Can you help me on how can I make AS recognize this class?
Thank you!

can u show me the script please

1 Like