写信给统一';Android上运行时的StreamingAssets文件夹?
因此,我的目标如下:写信给统一';Android上运行时的StreamingAssets文件夹?,android,video,unity3d,Android,Video,Unity3d,因此,我的目标如下: 下载带有http请求的.mp4视频 使用www.bytes and File.writealBytes()将其放入我的Android APK中的StreamingAssets文件夹中 使用我买的EasyMovieTexture电影插件在Android设备上播放 但是,当我尝试通过以下路径写入StreamingAssets文件夹时,会出现错误: jar:file:///data/app/com.catlard.testName-1.apk!/assets/ash.mp4 这
jar:file:///data/app/com.catlard.testName-1.apk!/assets/ash.mp4
这也是下面PlaceMovieInStreamingAssets函数中_debugString的第一个值。在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());
}
}
使用UnityEngine;
使用系统集合;
使用System.IO;
公共类StartVideoFromWeb:MonoBehavior{
公共字符串_movieFileName;
公共字符串\u movieHTTPLocation;
私有字符串_debugString;
私有媒体播放控制;
公共浮点数_mbFileSize=16.7f;
私有字符串_movieLocationIndevice;
私人浮动_prevProg;
私人电影WWW;
公共浮动(kb)速度;;
公共浮点数_peedmeasurements之间的时间=1f;
//用于初始化
公共IEnumerator Start(){
收益返回开始例程(“LoadMovie”);
将电影放入流程资产(_movieWWW);
返回新的WaitForSeconds(.5f);
//PlayMovieInCtrl(_movieHTTPLocation+_movieFileName);
PlayMovieInCtrl(_movieLocationOnDevice);
收益率为0;
}
public void PlayMovieInCtrl(字符串路径){
_控件=GetComponent();
_debugString=_movieFileName+“已加载到Ctrl”;
_控制。加载(路径);
renderer.material.mainTexture=_control.GetVideoTexture();
_控制。播放();
_debugString=“试图在“+path+”播放电影”;
}
公开作废电影流入资产(WWW){
_movieLocationOnDevice=“jar:file://“+Application.dataPath+”!/assets/”+\u movieFileName;
//_movieLocationOnDevice=Application.persistentDataPath+“/”+\u movieFileName;
_debugString=“未能写入路径:”+\u movieLocationNodeDevice;
writealBytes(_movieLocationOnDevice,www.bytes);
_debugString=“已将文件写入StreamingAssets文件夹。”;
}
公共IEnumerator MeasureSpeed(){
产生返回新的WaitForSeconds(_timeBetweenSpeedMeasurements);
浮点tempMeasure=0;/((_movieWWW.progress-_prevProg)*(_mbFileSize*1024f))*(_timebetween-peedMeasures/Time.deltaTime);
_prevProg=_movieWWW.progress;
如果(tempMeasure>0)
_kbSpeed=数学圆整度(tempMeasure);
开始例行程序(“测量速度”);
}
公共IEnumerator加载电影(){
字符串urlString=\u movieHTTPLocation+\u movieFileName;
_movieWWW=newwww(urlString);
bool isLoaded=false;
开始例行程序(“测量速度”);
而(!isLoaded&&u movieWWW.error==null){
_debugString=“Movie”+Mathf.RoundToInt(_movieWWW.progress*100f).ToString()+“%”;
如果(_kbSpeed>0)
_debugString+=“@”+\u kbSpeed.ToString()+“kb/sec”;
如果(_movieWWW.progress==1.0)
isLoaded=true;
收益率为0;
}
StopCorroutine(“测量速度”);
如果(_movieWWW.error==null)
_debugString=“加载的电影没有错误。”;
其他的
_debugString=_movieWWW.error.ToString();
}
公营机构{
标签(新的Rect(0,0700100),_debugString);
Label(新的Rect(0,30400100),“Buffered:”+_control.GetCurrentSeekPercent().ToString());
}
}
项目资产(StreamingAssets、RawAssets等)是只读的,你不能在那里写任何东西,而是写到内部/外部存储。看一看,如果你喜欢用c写,我相信你会使用Xaramin frameworkAhmad是正确的,它们是只读的。但是,您可以写入应用程序。persistentDataPath
我对流媒体MP3也做了同样的事情。我编写的以下脚本从internet获取数据(一个mp3文件),然后将其持久化到persistentDataPath并写入字节。如果文件已经存在,它将从本地路径而不是www获取
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
public class PlayMusic : MonoBehaviour {
public string[] songs;
public string currentSong;
public WebClient webClient;
public float Size = 0.0f;
private int xRand;
WWW request;
byte[] fileData;
IEnumerator loadAndPlayAudioClip(string song) {
string path = song;
currentSong = song;
FileInfo info = new FileInfo(Application.persistentDataPath + "/" + xRand + "_song.mp3");
if (info.Exists == true) {
print("file://"+Application.persistentDataPath + "/" + xRand + "_song.mp3 exists");
path = "file:///" + Application.persistentDataPath + "/" + xRand + "_song.mp3";
}
// Start a download of the given URL
request = new WWW(path);
yield return request;
AudioClip audioTrack = request.GetAudioClip(false, true);
audio.clip = audioTrack;
audio.Play();
Destroy(this);
}
void Start () {
xRand = Random.Range(0, 11);
StartCoroutine(loadAndPlayAudioClip(songs[xRand]));
}
// Update is called once per frame
void Update () {
print("Size: "+Size+" bytes");
if (request.isDone) {
fileData = request.bytes;
Size = fileData.Length;
if (fileData.Length > 0) {
File.WriteAllBytes(Application.persistentDataPath + "/" + xRand + "_song.mp3", fileData);
print("Saving mp3 to " + Application.persistentDataPath + "/" + xRand + "_song.mp3");
}
}
}
}
嗨@Apollo软件我遇到了与OP相同的问题,您的解决方案在Android上完美运行,但在iOS上不起作用。你知道如何让它在iOS上工作吗?提前谢谢!你知道有什么好笑的。直到大约6个月前,它还在iOS上运行。我想他们改变了什么。除了允许NSAppTransportSecurity NSAllowsArbityLoads之外,我不确定你还能做什么。我已经搞定了!在iOS上,将“file:///”更改为“file://”(“仅2'/”),它就可以工作了。更新你的答案,以便将来有人问同样的问题。我只是添加了一个“如果统一”的IOS。谢谢你的回答!