283 lines
6.6 KiB
C#
283 lines
6.6 KiB
C#
namespace QFramework
|
|
{
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.Networking;
|
|
|
|
public static class LocalAudioResUtil
|
|
{
|
|
public static string ToLocalAudioResName(this string selfFilePath)
|
|
{
|
|
return string.Format("LocalAudio:{0}", selfFilePath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ±¾µØÒôƵ¼ÓÔØÆ÷
|
|
/// </summary>
|
|
public class LocalAudioRes : Res
|
|
{
|
|
private string mFullPath;
|
|
private string mHashCode;
|
|
private object mRawAsset;
|
|
|
|
UnityWebRequest request;
|
|
public static LocalAudioRes Allocate(string name)
|
|
{
|
|
var res = SafeObjectPool<LocalAudioRes>.Instance.Allocate();
|
|
if (res != null)
|
|
{
|
|
res.AssetName = name;
|
|
res.SetUrl(name.Substring(11));
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public void SetDownloadProgress(int totalSize, int download)
|
|
{
|
|
|
|
}
|
|
|
|
public string LocalResPath
|
|
{
|
|
get { return string.Format("{0}{1}", AssetBundlePathHelper.PersistentDataPath4Photo, mHashCode); }
|
|
}
|
|
|
|
public virtual object RawAsset
|
|
{
|
|
get { return mRawAsset; }
|
|
}
|
|
|
|
public bool NeedDownload
|
|
{
|
|
get { return RefCount > 0; }
|
|
}
|
|
|
|
public string Url
|
|
{
|
|
get { return mFullPath; }
|
|
}
|
|
|
|
public int FileSize
|
|
{
|
|
get { return 1; }
|
|
}
|
|
|
|
public void SetUrl(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
return;
|
|
}
|
|
|
|
mFullPath = url;
|
|
mHashCode = string.Format("Photo_{0}", mFullPath.GetHashCode());
|
|
}
|
|
|
|
public override bool UnloadImage(bool flag)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public override bool LoadSync()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public override void LoadAsync()
|
|
{
|
|
if (!CheckLoadAble())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(mAssetName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
DoLoadWork();
|
|
}
|
|
|
|
private void DoLoadWork()
|
|
{
|
|
State = ResState.Loading;
|
|
|
|
OnDownLoadResult(true);
|
|
|
|
//¼ì²â±¾µØÎļþÊÇ·ñ´æÔÚ
|
|
/*
|
|
if (!File.Exists(LocalResPath))
|
|
{
|
|
ResDownloader.S.AddDownloadTask(this);
|
|
}
|
|
else
|
|
{
|
|
OnDownLoadResult(true);
|
|
}
|
|
*/
|
|
}
|
|
|
|
protected override void OnReleaseRes()
|
|
{
|
|
if (mAsset != null)
|
|
{
|
|
GameObject.Destroy(mAsset);
|
|
mAsset = null;
|
|
}
|
|
|
|
mRawAsset = null;
|
|
}
|
|
|
|
public override void Recycle2Cache()
|
|
{
|
|
SafeObjectPool<LocalAudioRes>.Instance.Recycle(this);
|
|
}
|
|
|
|
public override void OnRecycled()
|
|
{
|
|
|
|
}
|
|
|
|
public void DeleteOldResFile()
|
|
{
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
public void OnDownLoadResult(bool result)
|
|
{
|
|
if (!result)
|
|
{
|
|
OnResLoadFaild();
|
|
return;
|
|
}
|
|
|
|
if (RefCount <= 0)
|
|
{
|
|
State = ResState.Waiting;
|
|
return;
|
|
}
|
|
|
|
ResMgr.Instance.PushIEnumeratorTask(this);
|
|
}
|
|
|
|
//ÍêÈ«µÄWWW·½Ê½,Unity °ïÖú¹ÜÀíÎÆÀí»º´æ£¬²¢ÇÒЧÂÊÃ²ËÆ¸ü¸ß
|
|
// TODO:persistantPath ÓÃ read
|
|
public override IEnumerator DoLoadAsync(System.Action finishCallback)
|
|
{
|
|
AudioType type = AudioType.MPEG;
|
|
|
|
if (mFullPath.EndsWith("mp3"))
|
|
{
|
|
type = AudioType.MPEG;
|
|
}
|
|
else if (mFullPath.EndsWith("ogg"))
|
|
{
|
|
type = AudioType.OGGVORBIS;
|
|
}
|
|
else if (mFullPath.EndsWith("wav"))
|
|
{
|
|
type = AudioType.WAV;
|
|
}
|
|
using (UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(mFullPath, type))
|
|
{
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError(string.Format("Res:{0}, WWW Errors:{1}", mFullPath, request.error));
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
yield break;
|
|
}
|
|
else
|
|
{
|
|
// Convert the downloaded data to an AudioClip
|
|
AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
|
|
mAsset = clip;
|
|
}
|
|
|
|
if (RefCount <= 0)
|
|
{
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
State = ResState.Ready;
|
|
|
|
finishCallback();
|
|
}
|
|
|
|
protected override float CalculateProgress()
|
|
{
|
|
if (request == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return request.downloadProgress;
|
|
}
|
|
|
|
/*
|
|
public IEnumerator StartIEnumeratorTask2(Action finishCallback)
|
|
{
|
|
if (refCount <= 0)
|
|
{
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
yield break;
|
|
}
|
|
|
|
WWW www = new WWW("file://" + LocalResPath);
|
|
yield return www;
|
|
if (www.error != null)
|
|
{
|
|
Log.E("WWW Error:" + www.error);
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
yield break;
|
|
}
|
|
|
|
if (!www.isDone)
|
|
{
|
|
Log.E("NetImageRes WWW Not Done! Url:" + m_Url);
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
|
|
www.Dispose();
|
|
www = null;
|
|
|
|
yield break;
|
|
}
|
|
|
|
if (refCount <= 0)
|
|
{
|
|
OnResLoadFaild();
|
|
finishCallback();
|
|
|
|
www.Dispose();
|
|
www = null;
|
|
yield break;
|
|
}
|
|
|
|
TimeDebugger dt = new TimeDebugger("Tex");
|
|
dt.Begin("LoadingTask");
|
|
Texture2D tex = www.texture;
|
|
tex.Compress(true);
|
|
dt.End();
|
|
dt.Dump(-1);
|
|
|
|
m_Asset = tex;
|
|
www.Dispose();
|
|
www = null;
|
|
|
|
resState = eResState.kReady;
|
|
|
|
finishCallback();
|
|
}
|
|
*/
|
|
}
|
|
}
|