Hi I need to load a local ogg audio file (represented as a byte array) and convert it to an audio clip. I tried to access the file using the 'WWW' class but wasn't able to retrieve it correctly (it was corrupted) Is that even possible in WebGL ? Thanks!
this works for me: Code (CSharp): WWW data = new WWW (url); yield return data; try { AudioClip ac = data.GetAudioClipCompressed(false, AudioType.AUDIOQUEUE) as AudioClip; if(ac != null) { ac.name = "mySoundFile.ogg"; } else { Debug.Log("no audio found."); } } catch (System.Exception e) { Debug.Log(e); }
Hi sumpfkraut, This works for a url, but I need to load an audio file from the hard drive - not from a url
I can't seem to use WWW correctly to load a file that is stored locally on indexed db. The only thing I can think of is finding some kind of algorithm that will transform the byte array to an audio clip
url can be a file-path, web-adress, or blob. works for everything. for WebGL it's a blob. here is a example: https://www.expoformer.ch/fileadmin/static/wp/saveandload/
Can you point me for a working example on how to use the file path/blob option? I wasn't able to find any that works
Thanks, I checked it out, but there is no source code there, also the files are not saved in indexed db, you just save them yourself on the hard drive. What I'm looking for is something like this (2 options): 1.Read a locally saved audio file(ogg format) into a byte array (this works ok) 2.Convert the byte array to some float array that can be used to create an audio clip (no idea how to do this) --------or------------ 1.Read a locally saved audio file using the WWW class (I tried to do this but with no success) 2.Use the 'GetAudioClip' method to get the audio clip Hope this is clearer now
i hope this helps... In your C# code: Spoiler Code (CSharp): #if UNITY_WEBGL using System.Runtime.InteropServices; #endif ... ... #if UNITY_WEBGL [DllImport("__Internal")] private static extern void SoundUploader(string gameObjectName, string voidName); #endif private void LoadOgg() // <- Start this to load a ogg file from your hard drive. { #if UNITY_WEBGL SoundUploader(gameObject.name, "LoadOggInWebGL"); #endif } private IEnumerator LoadOggInWebGL(string url) // URL as BLOB from the jslib plugin (SoundUploader.jslib) { #if UNITY_WEBGL WWW data = new WWW(url); yield return data; try { AudioClip ac = data.GetAudioClipCompressed(false, AudioType.AUDIOQUEUE) as AudioClip; if (ac != null) { ac.name = "mySoundFile.ogg"; gameObject.GetComponent<AudioSource> ().clip = ac; } else { Debug.Log("no audio found."); } } catch (System.Exception e) { Debug.Log(e); } #endif } SoundUploader.jslib in the Assets/Plugins folder Spoiler Code (JavaScript): var SoundUploaderPlugin = { SoundUploader: function(gObj, vName) { var gameObj = Pointer_stringify(gObj); var voidName = Pointer_stringify(vName); if (!document.getElementById('SoundUploaderInput')) { var fileInput = document.createElement('input'); fileInput.setAttribute('type', 'file'); fileInput.setAttribute('id', 'SoundUploaderInput'); fileInput.setAttribute('accept', '.ogg'); fileInput.style.visibility = 'hidden'; fileInput.style.display = 'none'; fileInput.onclick = function (event) { this.value = null; }; fileInput.onchange = function (event) { blob = ((window.URL || window.webkitURL) || URL).createObjectURL(event.target.files[0]); gameInstance.SendMessage(gameObj, voidName, blob); document.getElementById("SoundUploaderInput").remove(); }; document.body.appendChild(fileInput); } else { //alert('SoundUploaderInput already exist...'); } var blob = null; var OpenFileDialog = function() { $("#SoundUploaderInput").click(); document.getElementById('gameContainer').removeEventListener('click', OpenFileDialog); }; document.getElementById('gameContainer').addEventListener('click', OpenFileDialog, false); } }; mergeInto(LibraryManager.library, SoundUploaderPlugin); - you have to click twice to get the file dialog in WebGL. EDIT it's possible that you can not use a IEnumerator direct from the .jslib then try this: Code (CSharp): private void LoadOggInWebGL(string url) { StartCoroutine(LoadOggInWebGL2(url)); } private IEnumerator LoadOggInWebGL2(string url) { //.... }
I can read the file with WWW now!! The audio clip is still not working but its huge progress for me, thanks
Hi, guys. I have online/offline project. I need to download wav/ogg/mp3 file from Application.persistentDataPath on WebGL platform. I tried www/webrequest. For example - WWW("file://" + Application.persistentDataPath + filePath); But always get error: Failed to load: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. Could you help me? P.S. From remote server works fine.
Unfortunately, Streaming Assets folder is located on the remote server and without internet I can't load data from there.