private void Play() { if (File.Exists(Settings.Selected.File.Path)) { // Set active file Settings.Active.File = Settings.Selected.File; // Reset selected file Settings.Selected.File = null; // Get active file FileObj file = Settings.Active.File; if (file != null) { // Play file if (Path.GetExtension(file.Path) == ".mp3") { clip = MP3Import.StartImport(file.Path); StartPlay(); } else { // Get audio resource WWW resource = new WWW("file:///" + file.Path.Replace('\\', '/').TrimStart(new char [] { '/' })); clip = resource.GetAudioClip(true, false); // Wait until file is loaded while (clip.loadState != AudioDataLoadState.Loaded) { if (clip.loadState == AudioDataLoadState.Failed) { Next(); return; } } if (clip != null && clip.length > 0) { StartPlay(); } else { Next(); } } } } else { // Try to play next file Next(); } }
private static AudioClip LoadMp3Clip(string filePath) { try { Utils.Log("Loading MP3 clip"); MP3Import importer = new MP3Import(); AudioClip clip = importer.StartImport(filePath); clip.name = Path.GetFileNameWithoutExtension(filePath); return(clip); } catch (Exception ex) { // TODO: Don't continually attempt to load failing tracks when they're the only one in the playlist. Debug.LogError("[STED] Error loading MP3 " + filePath + ": " + ex.Message + "\r\n" + ex.StackTrace); return(null); } }
protected void FileSelectedCallback(string path, string directory) { m_fileBrowser = null; // handle mp3 file if( mostRecentExtension == "mp3") { lastUsedDirectorymp3 = directory; filePathmp3 = path; if(filePathmp3 != null) { mp3Importer = (MP3Import)GetComponent("MP3Import"); mp3Importer.StartImport(filePathmp3); audioDirector.audioSourceArray[0].Stop(); audioDirector.audioSourceArray[0] = null; audioDirector.audioSourceArray[0] = mp3Importer.audioSource; audioDirector.audioSourceArray[0].Play(); audioDirector.currentlyPlayingFileName = Path.GetFileName(path); // fix for low pass filter not working on loaded songs audioListener.enabled = false; audioListener.enabled = true; // fixes memory leaked cause by unsed audio clips (occurs when loading new songs) Resources.UnloadUnusedAssets(); } } // handle parameters file else if( mostRecentExtension == "txt") { lastUsedDirectorytxt = directory; filePathtxt = path; // load parameters text file into string string rawFileString = File.ReadAllText(filePathtxt); string[] rawSplit = rawFileString.Split('|'); string amplitudeListString = rawSplit[1]; string frequencyStartString = rawSplit[2]; string frequencyListString = rawSplit[3]; string rgbColorScaleFactorsString = rawSplit[4]; // copy over amplitudes for(int i = 0; i < audioDirector.scalingPerDecadeArray.Length; i++) audioDirector.scalingPerDecadeArray[i] = float.Parse( amplitudeListString.Split(',')[i] ); // copy over frequency start index audioDirector.sampleStartIndex = int.Parse( frequencyStartString.Trim() ); // copy over frequency distribution for(int i = 0; i < audioDirector.samplesPerDecadeArray.Length; i++) audioDirector.samplesPerDecadeArray[i] = int.Parse( frequencyListString.Split(',')[i] ); // copy over rgb scale values audioDirector.rScale = float.Parse( rgbColorScaleFactorsString.Split(',')[0] ); audioDirector.gScale = float.Parse( rgbColorScaleFactorsString.Split(',')[1] ); audioDirector.bScale = float.Parse( rgbColorScaleFactorsString.Split(',')[2] ); } //isActive = false; }
// Use this for initialization void Start() { mp3Importer = (MP3Import)GetComponent("MP3Import"); audioDirector = (AudioDirectorScript) GameObject.Find("AudioDirector").GetComponent("AudioDirectorScript"); audioListener = (AudioListener) GameObject.Find("AudioDirector").GetComponent("AudioListener"); genralEditorScript = (GeneralEditorScript)GetComponent("GeneralEditorScript"); if(isActive) { Screen.showCursor = true; Screen.lockCursor = false; } }
private static AudioClip LoadMp3Clip(string filePath) { try { Utils.Log("Loading MP3 clip"); MP3Import importer = new MP3Import(); AudioClip clip = importer.StartImport(filePath); clip.name = Path.GetFileNameWithoutExtension(filePath); return clip; } catch (Exception ex) { // TODO: Don't continually attempt to load failing tracks when they're the only one in the playlist. Debug.LogError("[STED] Error loading MP3 " + filePath + ": " + ex.Message + "\r\n" + ex.StackTrace); return null; } }
/// <summary> /// Adds any MP3s in the SoundtrackEditory\Music\ directory to the GameDatabase. /// </summary> /// <remarks> /// Unity no longer supports loading MP3 tracks, giving the error: /// "Streaming of 'mp3' on this platform is not supported" /// Instead, this is achieved using the MPG123 utility to load the MP3 /// data unto a Unity AudioClip. For details, see here: /// http://answers.unity3d.com/questions/380838/is-there-any-converter-mp3-to-ogg-.html /// </remarks> private void LoadMp3s() { try { // Check all files in the Music directory; find the mp3s. Debug.Log("STED: Running MP3 importer..."); DirectoryInfo modDir = Directory.GetParent(AssemblyDirectory); string path = Path.Combine(modDir.FullName, "Music") + Path.DirectorySeparatorChar; foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) { string ext = Path.GetExtension(file); if (!String.IsNullOrEmpty(ext) && ext.Equals(".mp3", StringComparison.InvariantCultureIgnoreCase)) { MP3Import importer = new MP3Import(); AudioClip clip = importer.StartImport(file); // Set the clip name to match the format used for clips in the GameDatabase. string clipShortPath = Path.GetFileName(modDir.FullName) + file.Substring(modDir.FullName.Length, file.Length - modDir.FullName.Length - ".mp3".Length); if (Path.DirectorySeparatorChar == '\\') // Change Windows path separators to match the GameDatabase. clipShortPath = clipShortPath.Replace('\\', '/'); clip.name = clipShortPath; GameDatabase.Instance.databaseAudio.Add(clip); } } Debug.Log("STED: Finished importing MP3s."); } catch (Exception ex) { Debug.LogError("STED MP3 Import error: " + ex.Message); } }
public void AudioDBFileGui(AudioLoader.AudioFileInfo audioFile) { GUILayout.BeginHorizontal(); GUILayout.Label(" " + audioFile.Name); GUILayout.FlexibleSpace(); //string buttonText = " ► "; //if (DbPlayer.clip != null && DbPlayer.clip.name == clipName) //{ //if (DbPlayer.isPlaying) //buttonText = " ■ "; //else if (DbPlayer.clip.loadState == AudioDataLoadState.Loading) //buttonText = "..."; //} //if (GUILayout.Button(buttonText)) //{ //if (DbPlayer.isPlaying && DbPlayer.clip.name == clipName) //DbPlayer.Stop(); //else //{ //DbPlayer.clip = clip; //DbPlayer.Play(); //} //} if (!_expandedDbItems.ContainsKey(audioFile.Name)) _expandedDbItems.Add(audioFile.Name, false); if (GUILayout.Button(" ☰ ")) _expandedDbItems[audioFile.Name] = !_expandedDbItems[audioFile.Name]; GUILayout.EndHorizontal(); if (_expandedDbItems[audioFile.Name]) { GUILayout.Label(" Path:\t" + audioFile.Path); GUILayout.Label(" File type:\t" + audioFile.FileExtension); if (audioFile.FileExtension.ToUpper() == ".MP3") { if (Utils.LibMpg123Installed) { MP3Import.TrackInfo trackInfo; LoadedMP3Info.TryGetValue(audioFile.Path, out trackInfo); if (!LoadedMP3Info.ContainsKey(audioFile.Path)) { MP3Import importer = new MP3Import(); trackInfo = importer.GetTrackInfo(audioFile.Path); LoadedMP3Info.Add(audioFile.Path, trackInfo); } GUILayout.Label(" Title:\t\t" + trackInfo.Title); GUILayout.Label(" Album:\t\t" + trackInfo.Album); GUILayout.Label(" Artist:\t\t" + trackInfo.Artist); GUILayout.Label(" Year:\t\t" + trackInfo.Year); GUILayout.Label(" Genre:\t\t" + trackInfo.Genre); GUILayout.Label(" Comment:\t" + trackInfo.Comment); GUILayout.Label(" Tag:\t\t" + trackInfo.Tag); GUILayout.Label(" Channels:\t" + trackInfo.Channels); GUILayout.Label(" Encoding:\t" + trackInfo.Encoding); GUILayout.Label(" Rate:\t\t" + trackInfo.Rate); } else GUILayout.Label("MP3 support not installed"); } //GUILayout.Label(" Channels:\t" + clip.channels.ToString()); //GUILayout.Label(" Frequency:\t" + clip.frequency.ToString() + " Hz"); //TimeSpan clipLength = TimeSpan.FromSeconds(clip.length); //GUILayout.Label(" Length:\t\t" + string.Format("{0:D2}:{1:D2}", clipLength.Minutes, clipLength.Seconds)); //GUILayout.Label(" Load State:\t" + clip.loadState.ToString()); //GUILayout.Label(" Load Type:\t" + clip.loadType.ToString()); //GUILayout.Label(" Samples:\t" + clip.samples.ToString()); } }