Writing to Unity's StreamingAssets folder at runtime, on Android?
android, unity-game-engine, video
Solution
The project assests (StreamingAssets ,RawAssets,etc..) are read only, you cant write anything there, instead write write to internal/external storage have a look here, if you prefer writing in c# i believe that you would use Xaramin framework
Problem
So...my goal is the following: - Download an .mp4 video with an http request. - Put it in the StreamingAssets folder in my Android APK, using www.bytes and File.WriteAllBytes(). - Play it on an Android device, using the EasyMovieTexture movie plugin I bought. However, I'm getting errors when I try to write to the StreamingAssets folder, at the following path: ``` jar:file:///data/app/com.catlard.testName-1.apk!/assets/ash.mp4 ``` This is also the first value of _debugString in the PlaceMovieInStreamingAssets function below. Is it even possible to write to the StreamingAssets folder at runtime on Android? Here is the class I'm using to do this stuff. It stops on the WriteAllBytes call -- I know, because I've put print statements before and after, and that's where it's stopping. ``` using UnityEngine; using System.Collections; using System.IO; public class StartVideoFromWeb : MonoBehaviour { public string _movieFileName; public string _movieHTTPLocation; private string _debugString; private MediaPlayerCtrl _control; public float _mbFileSize = 16.7f; private string _movieLocationOnDevice; private float _prevProg; private WWW _movieWWW; public float _kbSpeed; public float _timeBetweenSpeedMeasurements = 1f; // Use this for initialization public IEnumerator Start() { yield return StartCoroutine("LoadMovie"); PlaceMovieInStreamingAssets(_movieWWW); yield return new WaitForSeconds(.5f); //PlayMovieInCtrl(_movieHTTPLocation + _movieFileName); PlayMovieInCtrl(_movieLocationOnDevice); yield return 0; } public void PlayMovieInCtrl(string path) { _control = GetComponent<MediaPlayerCtrl>(); _debugString = _movieFileName + " was loaded into Ctrl"; _control.Load(path); renderer.material.mainTexture = _control.GetVideoTexture(); _control.Play(); _debugString = "Attempted to play movie at " + path + "."; } public void PlaceMovieInStreamingAssets(WWW www) { _movieLocationOnDevice = "jar:file://" + Application.dataPath + "!/assets/" + _movieFileName; //_movieLocationOnDevice = Application.persistentDataPath + "/" + _movieFileName; _debugString = "Failed to write to path: " + _movieLocationOnDevice; File.WriteAllBytes(_movieLocationOnDevice, www.bytes); _debugString = "Wrote file to StreamingAssets folder."; } public IEnumerator MeasureSpeed() { yield return new WaitForSeconds(_timeBetweenSpeedMeasurements); float tempMeasure = 0;//((_movieWWW.progress - _prevProg) * (_mbFileSize * 1024f)) * (_timeBetweenSpeedMeasurements /Time.deltaTime); _prevProg = _movieWWW.progress; if(tempMeasure > 0) _kbSpeed = Mathf.RoundToInt(tempMeasure); StartCoroutine("MeasureSpeed"); } public IEnumerator LoadMovie () { string urlString = _movieHTTPLocation + _movieFileName; _movieWWW = new WWW(urlString); bool isLoaded = false; StartCoroutine("MeasureSpeed"); while(!isLoaded && _movieWWW.error == null) { _debugString = "Movie " + Mathf.RoundToInt(_movieWWW.progress * 100f).ToString() + "%"; if(_kbSpeed > 0) _debugString += " @ " + _kbSpeed.ToString() + "kb/sec"; if(_movieWWW.progress == 1.0) isLoaded = true; yield return 0; } StopCoroutine("MeasureSpeed"); if(_movieWWW.error == null) _debugString = "Movie loaded with no errors."; else _debugString = _movieWWW.error.ToString(); } public void OnGUI() { GUI.Label(new Rect(0,0,700,100), _debugString); GUI.Label(new Rect(0,30, 400, 100), "Buffered: " + _control.GetCurrentSeekPercent().ToString()); } } ```