| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Components |
| | 8 | | { |
| | 9 | | public class DCLAudioClip : BaseDisposable |
| | 10 | | { |
| | 11 | | [System.Serializable] |
| | 12 | | public class Model : BaseModel |
| | 13 | | { |
| | 14 | | public string url; |
| | 15 | | public bool loop = false; |
| 53 | 16 | | public bool shouldTryToLoad = true; |
| | 17 | |
|
| | 18 | | [Range(0f, 1f)] |
| 53 | 19 | | public double volume = 1f; |
| | 20 | |
|
| 18 | 21 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public AudioClip audioClip; |
| | 25 | | private bool isDisposed = false; |
| | 26 | |
|
| | 27 | | public enum LoadState |
| | 28 | | { |
| | 29 | | IDLE, |
| | 30 | | LOADING_IN_PROGRESS, |
| | 31 | | LOADING_FAILED, |
| | 32 | | LOADING_COMPLETED, |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public LoadState loadingState { get; private set; } |
| | 36 | | public event Action<DCLAudioClip> OnLoadingFinished; |
| | 37 | |
|
| 17 | 38 | | public DCLAudioClip() |
| | 39 | | { |
| 17 | 40 | | model = new Model(); |
| | 41 | |
|
| 17 | 42 | | loadingState = LoadState.IDLE; |
| 17 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | public double volume => ((Model) model).volume; |
| | 46 | |
|
| 0 | 47 | | public bool isLoop => ((Model) model).loop; |
| | 48 | |
|
| 0 | 49 | | public bool shouldTryLoad => ((Model) model).shouldTryToLoad; |
| | 50 | |
|
| 0 | 51 | | public override int GetClassId() { return (int) CLASS_ID.AUDIO_CLIP; } |
| | 52 | |
|
| | 53 | | void OnComplete(AudioClip clip) |
| | 54 | | { |
| 13 | 55 | | if (clip != null) |
| | 56 | | { |
| 13 | 57 | | this.audioClip = clip; |
| 13 | 58 | | loadingState = LoadState.LOADING_COMPLETED; |
| 13 | 59 | | } |
| | 60 | | else |
| | 61 | | { |
| 0 | 62 | | loadingState = LoadState.LOADING_FAILED; |
| | 63 | | } |
| | 64 | |
|
| 13 | 65 | | if (OnLoadingFinished != null) |
| | 66 | | { |
| 0 | 67 | | OnLoadingFinished.Invoke(this); |
| | 68 | | } |
| 13 | 69 | | } |
| | 70 | |
|
| | 71 | | void OnFail(string error) |
| | 72 | | { |
| 0 | 73 | | loadingState = LoadState.LOADING_FAILED; |
| | 74 | |
|
| 0 | 75 | | if (OnLoadingFinished != null) |
| | 76 | | { |
| 0 | 77 | | OnLoadingFinished.Invoke(this); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | IEnumerator TryToLoad() |
| | 82 | | { |
| 14 | 83 | | if (loadingState != LoadState.LOADING_IN_PROGRESS |
| | 84 | | && loadingState != LoadState.LOADING_COMPLETED) |
| | 85 | | { |
| 14 | 86 | | loadingState = LoadState.LOADING_IN_PROGRESS; |
| 14 | 87 | | Model model = (Model) this.model; |
| 14 | 88 | | if (scene.contentProvider.HasContentsUrl(model.url)) |
| | 89 | | { |
| 14 | 90 | | yield return Utils.FetchAudioClip( |
| | 91 | | scene.contentProvider.GetContentsUrl(model.url), |
| | 92 | | Utils.GetAudioTypeFromUrlName(model.url), |
| | 93 | | OnComplete, |
| | 94 | | OnFail); |
| | 95 | | } |
| | 96 | | } |
| 13 | 97 | | } |
| | 98 | |
|
| | 99 | | void Unload() |
| | 100 | | { |
| 0 | 101 | | if (audioClip != null && loadingState != LoadState.IDLE) |
| | 102 | | { |
| 0 | 103 | | audioClip.UnloadAudioData(); |
| 0 | 104 | | audioClip = null; |
| 0 | 105 | | loadingState = LoadState.IDLE; |
| | 106 | | } |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 110 | | { |
| 36 | 111 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 112 | |
|
| | 113 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 114 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 18 | 115 | | if (isDisposed) |
| 0 | 116 | | yield break; |
| | 117 | |
|
| 18 | 118 | | Model model = (Model) newModel; |
| | 119 | |
|
| 18 | 120 | | if (!string.IsNullOrEmpty(model.url)) |
| | 121 | | { |
| 14 | 122 | | if (model.shouldTryToLoad && audioClip == null) |
| | 123 | | { |
| 14 | 124 | | yield return TryToLoad(); |
| 13 | 125 | | } |
| 0 | 126 | | else if (!model.shouldTryToLoad && audioClip != null) |
| | 127 | | { |
| 0 | 128 | | Unload(); |
| | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| 17 | 132 | | yield return null; |
| 17 | 133 | | } |
| | 134 | |
|
| | 135 | | public override void Dispose() |
| | 136 | | { |
| 17 | 137 | | isDisposed = true; |
| 17 | 138 | | Utils.SafeDestroy(audioClip); |
| 17 | 139 | | base.Dispose(); |
| 17 | 140 | | } |
| | 141 | | } |
| | 142 | | } |