- Home /
Android build - videoplayer - cannot read .mp4 file
I am placing a video on my oculus quest headset and accessing it from the unity build. The idea was then to load the video into the video player and play it so that I could avoid having to download the file or build a unity project with a 8gb+ 360 video file.
This is the code that fetches the video on the headset.
string rootPath;
string path;
[SerializeField] string fileName;
[SerializeField] VideoPlayer _vp;
[SerializeField] TextMeshProUGUI _debugText;
bool readyToPlay = true;
void Start()
{
_vp = GetComponent<VideoPlayer>();
_vp.errorReceived += VideoPlayer_errorReceived;
if (Application.platform == RuntimePlatform.Android)
{
rootPath = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android", StringComparison.Ordinal));
path = Path.Combine(Path.Combine(rootPath, "Android/Data/_Videos"), fileName);
}
_debugText.text += rootPath + "\n";
_debugText.text += path + "\n";
if (File.Exists(path))
_debugText.text += "\nFile has been found!\n\n";
_vp.url = path;
if (readyToPlay)
{
_vp.Play();
}
}
private void VideoPlayer_errorReceived(VideoPlayer source, string message)
{
#if UNITY_EDITOR
Debug.Log(message);
#endif
readyToPlay = false;
_debugText.text += message;
/// To avoid memory leaks, unsubscribe from the event
/// otherwise it could continuously send this message
_vp.errorReceived -= VideoPlayer_errorReceived;
}
private void OnDisable()
{
_vp.errorReceived -= VideoPlayer_errorReceived;
}
}
It is finding the file but it is saying "cannot read file" as can be seen by this screenshot.
I have also made sure that the android.Manifest has the correct permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Im simply wondering why not when I can play this file in the editor no problem. Any input would be greatly appreciated.
Answer by Bettaxis · Oct 02, 2019 at 09:06 PM
@SteenPetersen your answer really helped me with the project I am working on. In the same vein of large 360 videos that are played through a Unity App for the Quest. I was not able to build .apks with the videos linked natively in the Unity VideoClip component as the .apk would be too huge and would result in Gradle errors, with this method, I just manually place the video files and link them using the URL, while excluding the videos from the actual Build and Project which results in a much smaller .apk that is easier to deploy. I modified your code and used this variation, along with the Android Manifest permission additions. VideoPlayer video;
private string rootPath; private string path; private string fileName = "NameOfYourVideo.mp4";
void Awake() { video = FindObjectOfType(); video.errorReceived += VideoPlayer_errorReceived;
if (Application.platform == RuntimePlatform.Android)
{
rootPath = Application.persistentDataPath;
path = Path.Combine(rootPath, fileName);
}
video.url = path;
video.Play();
}
This in the URL leads to the path in the Quest at "Internal shared storage\Android\data\com.CompanyName.ProjectName\files" where I placed the large videos I needed and with this method you can also update the filename in a script to change/load different videos. As for your issue with playing the video, I encountered that error as well and I was able to resolve it by making sure the target was in the com.CompanyName.ProjectName\Files instead of the Android\Data_Videos that you placed them in. I think this has to do with how Unity accesses files at runtime (see here), putting it in the com. folder makes it work and it plays flawlessly! Note that Above ~3GB videos don't seem to play on the Quest and I am unsure as to if it's a Quest limitation due to the 4GB of RAM or a Unity issue at this time. However, smaller 360 videos do work!
This is old but just came across it. So far as I know, the limit exists only with 32-bit builds. This is due to memory and file size limitations of 32-bit architecture. Using 64-bit builds should not have a problem with file sizes >4gb.