所以…我的目标是:
下载带有http请求的.mp4视频。
使用www.bytes和file.writeAllBytes()将其放入我的Android APK中的streamingassets文件夹。
使用我买的EasyMovieTexture电影插件在Android设备上播放。
但是,在尝试写入streamingassets文件夹时出现错误,路径如下:

jar:file:///data/app/com.catlard.testName-1.apk!/assets/ash.mp4

这也是下面placemovieinstreamingassets函数中调试字符串的第一个值。在android上运行时是否可以写入streamingassets文件夹?这是我用来做这些事情的课。它在writeAllBytes调用时停止——我知道,因为我在前后放置了print语句,这就是它停止的地方。
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());
    }
}

最佳答案

项目资产(streamingassets,rawassets,等等)是只读的,你不能在那里写任何东西,相反,写到内部/外部存储有一个外观here,如果你喜欢用c写,我相信你会使用xaramin框架

10-08 05:09