mave92
July 25, 2017, 11:38pm
1
Hi guys! I’m trying to load audio at runtime in WEBGL with UNITY PRO 5.6 but I’m losing my head trying to solve this error but it just won’t work.
error: streaming of “ogg” is not supported on this platform
This is what the script does:
1- Takes a URL pointing to a file (WWW)
2- Waits until the file is downloaded
3- Makes audioclip from dowloaded data (GetAudioClip (false, false, AudioType.OGGVORBIS)
4- Plays Audio (THIS IS WHERE IT FAILS!)
Everything is fine in Editor.
I tried every possible export combination and file format but nothing seems to work in browser.
PLEASE HELP!
mave92
July 29, 2017, 3:28am
2
Bump*
Does anyone have any idea?
TRY:
AudioClip ac = data.GetAudioClipCompressed(false, AudioType.AUDIOQUEUE) as AudioClip;
I tried this once, and it was working. only tested until unity 5.5
pearhaps this helps:
use it like this:
StartCoroutine(ReadWriteLocalFiles.ReadSound("", "", (returnClip) => { if(returnClip != null) { gameObject.GetComponent<AudioSource> ().clip = returnClip; } }));
other functions…
.jslib
var SoundUploaderPlugin = {
SoundUploader: function() {
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.onclick = function (event) {
this.value = null;
};
fileInput.onchange = function (event) {
SendMessage('SaveLoadScriptHolder', 'SoundFileSelected', URL.createObjectURL(event.target.files[0]));
}
document.body.appendChild(fileInput);
}
var OpenFileDialog = function() {
//document.getElementById('SoundUploaderInput').click();
$("#SoundUploaderInput").click()
document.getElementById('canvas').removeEventListener('click', OpenFileDialog);
};
document.getElementById('canvas').addEventListener('click', OpenFileDialog, false);
var isFirefox = typeof InstallTrigger !== 'undefined';
if (isFirefox) {
$("#canvas").click()
}
}
};
mergeInto(LibraryManager.library, SoundUploaderPlugin);
Called from jslib
//OGG
void SoundFileSelected (string url)
{
StartCoroutine(ReadWriteLocalFiles.LoadSoundWebGL(url, (returnClip) =>
{
if(returnClip != null) { gameObject.GetComponent<AudioSource> ().clip = returnClip; playButtonWebGL = true; }
else { gameObject.GetComponent<AudioSource> ().clip = null; playButtonWebGL = false; }
}));
}
ReadWriteLocalFiles.cs
#if UNITY_WEBGL
using System.Runtime.InteropServices;
#endif
//...
//.....
#if UNITY_WEBGL
[DllImport("__Internal")]
private static extern void SoundUploader();
#endif
//...
//.....
public static IEnumerator ReadSound (string directory, string fileName, System.Action<AudioClip> callback)
{
#if UNITY_STANDALONE
string directoryPath = GetDirectoryPath(directory);
string fullPath = System.IO.Path.Combine(directoryPath, fileName);
string extension = Path.GetExtension(fullPath);
AudioClip clip = null;
if(extension == ".ogg")
{
if (File.Exists(fullPath))
{
WWW www = new WWW("file://"+fullPath);
yield return www;
try
{
clip = www.GetAudioClip(false);
string[] parts = fullPath.Split('\\');
clip.name = parts[parts.Length - 1];
callback(clip);
}
catch(System.Exception e)
{
Debug.Log("error:"+e.Message);
callback(clip);
}
}
else
{
Debug.Log("file don't exist.");
callback(clip);
}
}
else
{
Debug.Log("invalid extension.");
callback(clip);
}
#elif UNITY_WEBGL
SoundUploader();
return null;
#else
Debug.Log("only aviable in webgl and standalone builds.");
return null;
#endif
}
public static IEnumerator LoadSoundWebGL (string url, System.Action<AudioClip> callback)
{
#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";
callback(ac);
}
else
{
Debug.Log("no audio found.");
callback(ac);
}
}
catch (System.Exception e)
{
Debug.Log(e);
AudioClip ac = null;
callback(ac);
}
#else
Debug.Log("only for webgl."); return null;
#endif
}