// Finalize the clip (name / SLPack), and try to replace global game audio if one has the same name. internal static AudioClip FinalizeAudioClip(AudioClip clip, string name, SLPack pack = null) { clip.name = name; if (pack != null) { if (pack.AudioClips.ContainsKey(name)) { SL.LogWarning("Replacing clip '" + name + "' in pack '" + pack.Name + "'"); if (pack.AudioClips[name]) { GameObject.Destroy(pack.AudioClips[name]); } pack.AudioClips.Remove(name); } pack.AudioClips.Add(name, clip); } if (Enum.TryParse(name, out GlobalAudioManager.Sounds sound)) { ReplaceAudio(sound, clip); } return(clip); }
private static IEnumerator LoadAudioFromFileCoroutine(string filePath, SLPack pack, Action <AudioClip> onClipLoaded) { var name = Path.GetFileNameWithoutExtension(filePath); var request = UnityWebRequestMultimedia.GetAudioClip($@"file://{Path.GetFullPath(filePath)}", AudioType.WAV); yield return(request.SendWebRequest()); while (!request.isDone) { yield return(null); } if (!string.IsNullOrEmpty(request.error)) { SL.LogWarning(request.error); yield break; } SL.Log($"Loaded audio clip: {Path.GetFileName(filePath)}"); var clip = DownloadHandlerAudioClip.GetContent(request); FinalizeAudioClip(clip, name, pack); onClipLoaded?.Invoke(clip); }
/// <summary> /// Load an Audio Clip from a given file path (on disk), and optionally put it in the provided SL Pack. /// </summary> /// <param name="filePath">The file path (must be a .WAV file) to load from</param> /// <param name="pack">Optional SL Pack to put the audio clip inside.</param> /// <param name="onClipLoaded">Event invoked when the AudioClip has finished loading, if successful.</param> /// <returns>The loaded audio clip, if successful.</returns> public static void LoadAudioClip(string filePath, SLPack pack = null, Action <AudioClip> onClipLoaded = null) { if (!File.Exists(filePath)) { return; } //var data = File.ReadAllBytes(filePath); //return LoadAudioClip(data, Path.GetFileNameWithoutExtension(filePath), pack); SLPlugin.Instance.StartCoroutine(LoadAudioFromFileCoroutine(filePath, pack, onClipLoaded)); }
/// <summary> /// Load an Audio Clip from a given byte array, and optionally put it in the provided SL Pack.<br/><br/> /// WARNING: AudioClips loaded from byte arrays are currently unreliable and may be glitched, use at own risk! /// </summary> /// <param name="data">The byte[] array from <see cref="File.ReadAllBytes(string)"/> on the wav file path.</param> /// <param name="name">The name to give to the audio clip.</param> /// <param name="pack">Optional SL Pack to put the audio clip inside.</param> /// <returns>The loaded audio clip, if successful.</returns> public static AudioClip LoadAudioClip(byte[] data, string name, SLPack pack = null) { SL.LogWarning("WARNING: AudioClips loaded from embedded .zip archives are currently unreliable and may be glitched, use at own risk!"); try { var clip = ConvertByteArrayToAudioClip(data, name); if (!clip) { return(null); } return(FinalizeAudioClip(clip, name, pack)); } catch (Exception ex) { SL.LogWarning("Exception loading AudioClip!"); SL.LogInnerException(ex); return(null); } }