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