aeroson

Untitled

Aug 30th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. namespace RuntimeAudioClipLoader
  6. {
  7. [RequireComponent(typeof(AudioSource))]
  8. public class DemoLoadAudioFromStreamingAssets : MonoBehaviour
  9. {
  10. public bool loadInBackground = true;
  11. public bool doStream = false;
  12.  
  13. // soundcloud stream link generated with https://www.appendipity.com/get-mp3-links-soundcloud/
  14. public string urlToLoad = @"http://media.soundcloud.com/stream/dP7wRLjm32Eh.mp3";
  15.  
  16. AudioSource audioSource;
  17. void Start()
  18. {
  19. audioSource = GetComponent<AudioSource>();
  20. }
  21. void OnGUI()
  22. {
  23. var sourceFolder = Path.Combine(Application.streamingAssetsPath, "RuntimeAudioClipLoader demo StreamingAssets");
  24. int num = 0;
  25. var files = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
  26. foreach(var file in files)
  27. {
  28. if (!file.EndsWith(".meta"))
  29. {
  30. if (num > 10) break;
  31. num++;
  32. if (GUILayout.Button("Load: " + file.Substring(sourceFolder.Length)))
  33. {
  34. audioSource.clip = RuntimeAudioClipLoader.Manager.Load(file, doStream, loadInBackground);
  35. }
  36. }
  37. }
  38.  
  39. GUILayout.Space(10);
  40.  
  41. GUILayout.Label("Url to load:");
  42. urlToLoad = GUILayout.TextArea(urlToLoad);
  43. if(GUILayout.Button("Load from url"))
  44. {
  45. StartCoroutine(DownloadClipFromUrl(urlToLoad));
  46. }
  47.  
  48. GUILayout.Space(10);
  49.  
  50. if (audioSource.clip)
  51. {
  52. GUILayout.Label("AudioClip.name: " + audioSource.clip.name);
  53. GUILayout.Label("AudioDataLoadState: " + RuntimeAudioClipLoader.Manager.GetAudioClipLoadState(audioSource.clip));
  54. GUILayout.Label("AudioClipLoadType: " + RuntimeAudioClipLoader.Manager.GetAudioClipLoadType(audioSource.clip));
  55.  
  56. GUILayout.Label("time: " + audioSource.time + "/" + audioSource.clip.length);
  57. if (GUILayout.Button("Play")) audioSource.Play();
  58. }
  59.  
  60. doStream = GUILayout.Toggle(doStream, "doStream (audioClip is loaded on the fly on demand, use for long one time use clips)");
  61. loadInBackground = GUILayout.Toggle(loadInBackground, "loadInBackground (if !doStream and loadInBackground then loading is done in own thread so it doesnt hang up caller's thread)");
  62.  
  63. if (GUILayout.Button("Clear cache (so you can test loading times again)"))
  64. {
  65. RuntimeAudioClipLoader.Manager.ClearCache();
  66. audioSource.clip = null;
  67. }
  68.  
  69. }
  70.  
  71.  
  72. IEnumerator DownloadClipFromUrl(string url)
  73. {
  74. // Debug.Log("loading " + url);
  75. WWW www = new WWW(url);
  76. yield return www;
  77. Debug.Log("loaded " + url + " got bytes:" + www.bytes.Length);
  78. var stream = new MemoryStream(www.bytes);
  79. var format = RuntimeAudioClipLoader.Manager.GetAudioFormat(url);
  80. if (format == AudioFormat.unknown) format = AudioFormat.mp3;
  81. audioSource.clip = RuntimeAudioClipLoader.Manager.Load(stream, format, url, doStream, loadInBackground);
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment