Hi, i’m developing a mobile application (iOS/Android) with some long mp3 and wav files (>10 min).
I’d like to put the mp3 file on a server and at the first app opening, the user can download the songs via www features.
WWW www = new WWW(url);
yield return www;
What to do with www.audioClip???
But i don’t know how to save the .mp3 file into the app to avoid downloading the song everytime, and how to load it later.
Thanks
Leo
The ‘bytes’ property of the www after it’s been completed contains the actual raw data:
You can save that with any standard method that one saves a byte array. Like File.WriteAllBytes:
Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is truncated and overwritten.
WWW www = new WWW(url);
yield return www;
System.IO.File.WriteAllBytes(@"C:\SomeFolder\SomeAudioClip.mp3", www.bytes);
1 Like
Thanks a lot.
And to load it once the app has been closed and reopen? I should use :
?
Thanks
sure…
Are you doing this so that the mp3 gets cached so the next time you go to get it, you don’t have to dl it again?
Note that AssetBundles can be downloaded and cached, which will do this for you (you’d distribute your mp3’s in an AssetBundle). They’re nice because you can version AssetBundles as well so you can flag mandatory updates to the AssetBundle through the AssetBundle version. See:
Yes exactly. I’ll work on it tomorow.
Thanks for the advice lordofduct
I would assume you can do this with asset bundles as well. Cache the bundle and load it.
I tried something like that :
public AudioClip audiolist;
IEnumerator getsounds () {
while (!Caching.ready) yield return null;
var www = WWW.LoadFromCacheOrDownload("http://www.myurl.com/mysound.wav", 5);
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
//return;
}
var myLoadedAssetBundle = www.assetBundle;
var asset = myLoadedAssetBundle.mainAsset;
audiolist=asset.audioClip;
Debug.Log("Load over");
But it doesn’t work, not sure how tu use “assetBundle” to get the audioclip.
I also tried write and readallbyte.
WWW www = new WWW("http://www.myurl.com/mysound.wav");
yield return www;
audiolist=www.audioClip;
Debug.Log("Download over");
System.IO.File.WriteAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav", www.bytes);
Working well, i can see my file into my Resources folder.
Then when i’m trying to load it :
byte[] levelData = System.IO.File.ReadAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav");//full local save file
float[] f = ConvertByteToFloat(levelData);
audiolist=AudioClip.Create("testSound", f.Length, 1, 44100, false, false);
audiolist.GetData(f, 0);
Function ConvertBytoFloat :
private float[] ConvertByteToFloat(byte[] array)
{
float[] floatArr = new float[array.Length / 4];
for (int i = 0; i < floatArr.Length; i++)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(array, i * 4, 4);
floatArr[i] = BitConverter.ToSingle(array, i * 4);
}
return floatArr;
}
Using unity 5.3 Free
To read the audio from cache use following snippet
WWW www = new WWW("file:///" + [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Application']Application[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=dataPath']dataPath[/URL] + "/../Assets/Ressources/audio1.wav");
yield return www;
audiolist=www.audioClip;
Debug.Log("Loading audio over");
Tarzan111:
I tried something like that :
public AudioClip audiolist;
IEnumerator getsounds () {
while (!Caching.ready) yield return null;
var www = WWW.LoadFromCacheOrDownload("http://www.myurl.com/mysound.wav", 5);
yield return www;
if(!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
//return;
}
var myLoadedAssetBundle = www.assetBundle;
var asset = myLoadedAssetBundle.mainAsset;
audiolist=asset.audioClip;
Debug.Log("Load over");
But it doesn’t work, not sure how tu use “assetBundle” to get the audioclip.
I also tried write and readallbyte.
WWW www = new WWW("http://www.myurl.com/mysound.wav");
yield return www;
audiolist=www.audioClip;
Debug.Log("Download over");
System.IO.File.WriteAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav", www.bytes);
Working well, i can see my file into my Resources folder.
Then when i’m trying to load it :
byte[] levelData = System.IO.File.ReadAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav");//full local save file
float[] f = ConvertByteToFloat(levelData);
audiolist=AudioClip.Create("testSound", f.Length, 1, 44100, false, false);
audiolist.GetData(f, 0);
Function ConvertBytoFloat :
private float[] ConvertByteToFloat(byte[] array)
{
float[] floatArr = new float[array.Length / 4];
for (int i = 0; i < floatArr.Length; i++)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(array, i * 4, 4);
floatArr[i] = BitConverter.ToSingle(array, i * 4);
}
return floatArr;
}
Using unity 5.3 Free
You do not have to write so much stories…Unity solved this…just get your audio from UnityWebRiquest.
IEnumerator GetAudioClip()
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("http://www.my-server.com/audio.ogg", AudioType.OGGVORBIS))
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
}
}
}
schashm3 schashm3:
You do not have to write so much stories…Unity solved this…just get your audio from UnityWebRiquest.
IEnumerator GetAudioClip()
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("http://www.my-server.com/audio.ogg", AudioType.OGGVORBIS))
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
}
}
}
and write this replace DownloadOrLoadFromCash…this is obsolete…
if(!System.IO.File.Exist("path")){
//downloadAudio()
}else{
StartCoroutine(GetAudioClip());
}