| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class AssetPromise_AudioClip : AssetPromise<Asset_AudioClip> |
| | 8 | | { |
| | 9 | | private readonly string url; |
| | 10 | | private readonly string id; |
| | 11 | | private readonly AudioType audioType; |
| | 12 | | private readonly IWebRequestController webRequestController; |
| | 13 | |
|
| | 14 | | private WebRequestAsyncOperation webRequestAsyncOperation; |
| | 15 | |
|
| 54 | 16 | | public AssetPromise_AudioClip(string clipPath, ContentProvider provider) : this(clipPath, provider, Environment. |
| | 17 | |
|
| 27 | 18 | | public AssetPromise_AudioClip(string clipPath, ContentProvider provider, IWebRequestController webRequestControl |
| | 19 | | { |
| 27 | 20 | | this.url = provider.GetContentsUrl(clipPath); |
| 27 | 21 | | this.id = this.url != null ? this.url.Substring(this.url.LastIndexOf('/') + 1) : $"{GetHashCode()}"; |
| 27 | 22 | | this.audioType = GetAudioTypeFromUrlName(clipPath); |
| 27 | 23 | | this.webRequestController = webRequestController; |
| 27 | 24 | | } |
| | 25 | |
|
| | 26 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 27 | | { |
| 26 | 28 | | if (string.IsNullOrEmpty(url)) |
| | 29 | | { |
| 0 | 30 | | OnFail?.Invoke(new Exception("Audio clip url is null or empty")); |
| 0 | 31 | | return; |
| | 32 | | } |
| | 33 | |
|
| 26 | 34 | | webRequestAsyncOperation = webRequestController.GetAudioClip(url, audioType, |
| | 35 | | request => |
| | 36 | | { |
| 20 | 37 | | asset.audioClip = DownloadHandlerAudioClip.GetContent(request.webRequest); |
| 20 | 38 | | OnSuccess?.Invoke(); |
| 20 | 39 | | }, |
| | 40 | | request => |
| | 41 | | { |
| 3 | 42 | | OnFail?.Invoke(new Exception($"Audio clip failed to fetch: {request?.webRequest?.error}")); |
| 3 | 43 | | }); |
| 26 | 44 | | } |
| | 45 | |
|
| | 46 | | protected override void OnCancelLoading() |
| | 47 | | { |
| 6 | 48 | | webRequestAsyncOperation?.Dispose(); |
| 6 | 49 | | webRequestAsyncOperation = null; |
| 6 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override bool AddToLibrary() |
| | 53 | | { |
| 20 | 54 | | if (!library.Add(asset)) |
| | 55 | | { |
| 0 | 56 | | return false; |
| | 57 | | } |
| 20 | 58 | | asset = library.Get(asset.id); |
| 20 | 59 | | return true; |
| | 60 | | } |
| | 61 | |
|
| 29 | 62 | | protected override void OnBeforeLoadOrReuse() { } |
| | 63 | |
|
| 23 | 64 | | protected override void OnAfterLoadOrReuse() { } |
| | 65 | |
|
| | 66 | | public override object GetId() |
| | 67 | | { |
| 145 | 68 | | return id; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | private static AudioType GetAudioTypeFromUrlName(string url) |
| | 72 | | { |
| 27 | 73 | | if (string.IsNullOrEmpty(url)) |
| | 74 | | { |
| 0 | 75 | | Debug.LogError("GetAudioTypeFromUrlName >>> Null url!"); |
| 0 | 76 | | return AudioType.UNKNOWN; |
| | 77 | | } |
| | 78 | |
|
| 27 | 79 | | string ext = url.Substring(url.Length - 3).ToLower(); |
| | 80 | |
|
| | 81 | | switch (ext) |
| | 82 | | { |
| | 83 | | case "mp3": |
| 0 | 84 | | return AudioType.MPEG; |
| | 85 | | case "wav": |
| 9 | 86 | | return AudioType.WAV; |
| | 87 | | case "ogg": |
| 15 | 88 | | return AudioType.OGGVORBIS; |
| | 89 | | default: |
| 3 | 90 | | return AudioType.UNKNOWN; |
| | 91 | | } |
| | 92 | | } |
| | 93 | | } |
| | 94 | | } |