public void Unload() { if (!loaded) return; AudioClip?.UnloadAudioData(); Object.Destroy(AudioClip); AudioClip = null; if (nLayerLoader != null) { nLayerLoader.Dispose(); nLayerLoader = null; } loaded = false; }
public async UniTask Load() { if (loaded) { Unload(); } var type = AudioTypeExtensions.Detect(Path); // TODO: Load remote mp3 with non-mobile platform (Download with UnityWebRequest first?) // Load .mp3 with NLayer on non-mobile platforms if ( Path.StartsWith("file://") && type == AudioType.MPEG && Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer ) { nLayerLoader = new NLayerLoader(Path); AudioClip = nLayerLoader.LoadAudioClip(); } else { using (var request = UnityWebRequestMultimedia.GetAudioClip(Path, type)) { await request.SendWebRequest(); if (request.isNetworkError || request.isHttpError) { Error = request.error; return; } AudioClip = DownloadHandlerAudioClip.GetContent(request); } } loaded = true; }
public async Task <VideoLoadingResponse> LoadVideo(string videoName, string mode) { VideoLoadingResponse response = new VideoLoadingResponse(); response.ok = true; response.errorMessage = ""; Haze.Logger.Log("Loading video " + videoName + "..."); errorWhileLoading = false; if (!IsVideoAvailable(videoName)) { response.errorMessage = "Video unavailable."; response.ok = false; Haze.Logger.LogError("Video " + videoName + " could not be found."); return(response); } is360 = false; if (mode.Length >= 3 && mode[0] == '3' && mode[1] == '6' && mode[2] == '0') { //360 video. is360 = true; Haze.Logger.Log("Loading 360 degree video."); } else { Haze.Logger.Log("Loading 235 degree video."); } //Check if we need to play binaural audio string leftAudioFile = getAudioPath(videoName, true); string rightAudioFile = getAudioPath(videoName, false); //determine extension based on whether the Wave file exists otherwise fallback onto mp3 if (File.Exists(leftAudioFile + ".wav")) { leftAudioFile += ".wav"; } else { leftAudioFile += ".mp3"; } if (File.Exists(rightAudioFile + ".wav")) { rightAudioFile += ".wav"; } else { rightAudioFile += ".mp3"; } //resources needed for loading audio WWW leftWWW = null; WWW rightWWW = null; AudioClip leftClip = null; AudioClip rightClip = null; UnityWebRequest leftuwr = null; UnityWebRequest rightuwr = null; if (File.Exists(leftAudioFile) && File.Exists(rightAudioFile)) { BinauralAudio = true; //load the sound into the audio sources switch (audioLoadingMode) { case AudioLoadingMode.WWW: leftWWW = new WWW("file://" + leftAudioFile.Replace('\\', '/')); rightWWW = new WWW("file://" + rightAudioFile.Replace('\\', '/')); break; case AudioLoadingMode.NLayer: leftClip = NLayerLoader.LoadMp3(leftAudioFile); rightClip = NLayerLoader.LoadMp3(rightAudioFile); break; case AudioLoadingMode.UnityWebResource: leftuwr = UnityWebRequestMultimedia.GetAudioClip("file://" + leftAudioFile.Replace('\\', '/'), AudioType.UNKNOWN); rightuwr = UnityWebRequestMultimedia.GetAudioClip("file://" + rightAudioFile.Replace('\\', '/'), AudioType.UNKNOWN); leftuwr.Send(); rightuwr.Send(); break; } } else { BinauralAudio = false; } Haze.Logger.Log("Binaural audio: " + (BinauralAudio ? "on" : "off")); if (BinauralAudio) { Haze.Logger.Log("Loading binaural audio files: " + leftAudioFile + " and " + rightAudioFile); } //Prepare video player player.OpenMedia(new MediaPath(getPath(videoName), MediaPathType.RelativeToPersistentDataFolder), autoPlay: false); PlaybackSpeed = 1; lastReadyFrame = -1; //Load the video into the player... DateTime before = DateTime.Now; TimeSpan took = DateTime.Now - before; Haze.Logger.Log("Player is prepared! Took: " + took.TotalMilliseconds + " ms."); //Wait for the audio sources to load if (BinauralAudio) { Haze.Logger.Log("Waiting for audio files to load..."); switch (audioLoadingMode) { case AudioLoadingMode.WWW: while (!leftWWW.isDone || !rightWWW.isDone) { await Task.Delay(20); } leftAudio.clip = leftWWW.GetAudioClip(); rightAudio.clip = rightWWW.GetAudioClip(); while (leftAudio.clip.loadState == AudioDataLoadState.Loading || rightAudio.clip.loadState == AudioDataLoadState.Loading) { await Task.Delay(20); } break; case AudioLoadingMode.NLayer: while (leftClip.loadState == AudioDataLoadState.Loading || rightClip.loadState == AudioDataLoadState.Loading) { await Task.Delay(20); } leftAudio.clip = leftClip; rightAudio.clip = rightClip; break; case AudioLoadingMode.UnityWebResource: while (!leftuwr.isDone || !rightuwr.isDone) { await Task.Delay(20); } leftAudio.clip = DownloadHandlerAudioClip.GetContent(leftuwr); rightAudio.clip = DownloadHandlerAudioClip.GetContent(rightuwr); if (leftuwr.isNetworkError) { Haze.Logger.LogError("Could not open audio file " + leftAudioFile + ": " + leftuwr.error); } if (rightuwr.isNetworkError) { Haze.Logger.LogError("Could not open audio file " + rightAudioFile + ": " + rightuwr.error); } break; } Haze.Logger.Log("Audio loaded: Left: " + leftAudio.clip.loadState + " / Right: " + rightAudio.clip.loadState); } Reorient(Vector3.zero); //reset orientation //Show first frame as soon as it's loaded in and rendered, making sure the video is 100% paused when we do so. float previousVolume = Volume; blackScreen.SetActive(false); if (is360) { spherePlayer.SetActive(true); } else { semispherePlayer.SetActive(true); } return(response); }