You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.8 KiB
97 lines
2.8 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
// Unity www.GetAudioClip() only support wav
|
|
public class SoundPool : PoolBase<SoundPool>
|
|
{
|
|
public Dictionary<string, AudioClip> sound_collection = new Dictionary<string, AudioClip>();
|
|
public AudioClip GetAudioClip(string filename)
|
|
{
|
|
if (sound_collection.ContainsKey(filename) == false)
|
|
{
|
|
var info = GetFileInfo(filename);
|
|
string path = info.FullName;
|
|
string ext = info.Extension;
|
|
#if false
|
|
AudioClip clip = null;
|
|
var www = new WWW("file://" + path);
|
|
while (www.isDone == false) { }
|
|
|
|
if (www.bytes.Length > 0)
|
|
{
|
|
clip = www.GetAudioClip(false, false, AudioType.AIFF);
|
|
clip.name = filename;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
www.Dispose();
|
|
sound_collection.Add(filename, clip);
|
|
#else
|
|
sound_collection.Add(filename, null);
|
|
StartCoroutine(LoadAudio(path, filename, ext));
|
|
#endif
|
|
}
|
|
return sound_collection[filename];
|
|
}
|
|
|
|
IEnumerator LoadAudio(string path, string filename, string extension)
|
|
{
|
|
#if false
|
|
var www = new WWW("file://" + path);
|
|
yield return www;
|
|
|
|
if (www.bytes.Length > 0)
|
|
{
|
|
var clip = www.GetAudioClip(false);
|
|
clip.name = filename;
|
|
sound_collection[filename] = clip;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
www.Dispose();
|
|
#else
|
|
string uri = "file://" + path;
|
|
string ext = GetFileInfo(filename).Extension;
|
|
var type = AudioType.UNKNOWN;
|
|
var comparison = System.StringComparison.CurrentCultureIgnoreCase;
|
|
if (ext.Equals(".aif", comparison))
|
|
type = AudioType.AIFF;
|
|
else if (ext.Equals(".wav", comparison))
|
|
type = AudioType.WAV;
|
|
else if (ext.Equals(".acc", comparison))
|
|
type = AudioType.ACC;
|
|
else if (ext.Equals(".ogg", comparison))
|
|
type = AudioType.OGGVORBIS;
|
|
|
|
print(type);
|
|
|
|
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(uri, type))
|
|
{
|
|
#if UNITY_2017_1
|
|
yield return www.Send();
|
|
#else
|
|
yield return www.SendWebRequest();
|
|
#endif
|
|
if (www.isNetworkError)
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
else
|
|
{
|
|
AudioClip clip = DownloadHandlerAudioClip.GetContent(www);
|
|
clip.name = filename;
|
|
print("downloadedBytes: " + www.downloadedBytes);
|
|
//clip.LoadAudioData();
|
|
sound_collection[filename] = clip;
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
}
|
|
|