在运行时是否在Android上写入Unity的StreamingAssets文件夹? [英] Writing to Unity's StreamingAssets folder at runtime, on Android?

查看:463
本文介绍了在运行时是否在Android上写入Unity的StreamingAssets文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我的目标是:

So...my goal is the following:

  1. 通过http请求下载.mp4视频.
  2. 使用www.bytes和File.WriteAllBytes()将其放在Android APK的StreamingAssets文件夹中.
  3. 使用我购买的EasyMovieTexture电影插件在Android设备上播放.

但是,尝试在以下路径上写入StreamingAssets文件夹时出现错误:

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

这也是下面的PlaceMovieInStreamingAssets函数中_debugString的第一个值.甚至可以在运行时在Android上写入StreamingAssets文件夹吗?这是我用来做这些事情的课程.它停止在WriteAllBytes调用上-我知道,因为我在前后都放置了打印语句,而这正是停止的地方.

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());
    }
}

推荐答案

项目资产(StreamingAssets,RawAssets等)是只读的,您不能在其中写任何东西,而是写写入内部/外部存储看看这里,如果您喜欢用C#编写,我相信您会使用Xaramin框架

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

这篇关于在运行时是否在Android上写入Unity的StreamingAssets文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆