Creating a Custom Music folder - Works inside the editor - Issues with build.
Hi all,
I'm having an issue with the audio script that I wrote ( the idea is to have a custom music folder so the user can customise it however they see fit) It works fine within the editor, and if I build it and add the same songs as were in the editor it runs fine. However if I add songs that weren't originally in the editor asset folder, then I run into issues.
This is my current code for my script. Apologies if it's messy, i've tried everything to get this working and now it looks like a train wreck.
The error I am getting is "Error: Cannot create FMOD::Sound instance for clip "" (FMOD error: Couldn't perform seek operation. This is a limitation of the medium (ie netstreams) or the file format. ) UnityEngine.Networking.DownloadHandlerAudioClip:GetContent(UnityWebRequest) d__6:MoveNext() (at Assets/Scripts/Audio Script/FindAudioInDirectory.cs:80) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)"
I am using the file type .ogg , and converted from mp3's to ogg using ffmpeg (Tried audacity aswell to see if something was up with the conversion, but again no success)
(also getting an index outside the bounds of the array but thats probably due to my never-ending tweaking of the code)
void Start()
{
//FindAudio();
StartCoroutine(GetAudio());
//FindAudioLength(source.clip);
}
#region Find and Play Audio
IEnumerator GetAudio()
{
if (Application.isEditor)
{
// directory = new DirectoryInfo(Application.dataPath + "/Music");
// Debug.Log(directory); // this commented code will work fine because its in the editor build.
//Files = Directory.GetFiles(Application.dataPath + "/Music");
directory = new DirectoryInfo("E:/UnityBuilds/CircleShooter");
Debug.Log(directory); // This doesn't work. FMOD error
Files = Directory.GetFiles("E:/UnityBuilds/CircleShooter");
}
else
{
directory = new DirectoryInfo(Directory.GetCurrentDirectory() + "/Music");
Debug.Log(directory);
Files = Directory.GetFiles(Directory.GetCurrentDirectory() + "/Music");
}
Debug.Log("L33");
int i = 0;
foreach(var file in directory.GetFiles("*",SearchOption.AllDirectories))
{
Debug.Log("l37");
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(Files[i], AudioType.OGGVORBIS))
{
yield return www.SendWebRequest();
if(www.isNetworkError)
{
Debug.LogError("Error Importing the Audio File");
}
else
{
if (Files[i].EndsWith(".ogg"))
Clips.Add(DownloadHandlerAudioClip.GetContent(www));
}
}
i++;
}
int clipNum = 0;
foreach (AudioClip clip in Clips)
{
clipNum++;
clip.name = "Song " + clipNum;
}
source.clip = Clips[UnityEngine.Random.Range(0,Clips.Count)];
source.Play();
FindAudioLength(source.clip);
}
void FindAudioLength(AudioClip clip)
{
CurrentSongLength = clip.length;
StartCoroutine(QueueNextSong());
}
IEnumerator QueueNextSong()
{
Debug.Log("Coroutine " + CurrentSongLength);
yield return new WaitForSeconds(CurrentSongLength);
source.clip = Clips[UnityEngine.Random.Range(0, Clips.Count)];
source.Play();
Debug.Log("Coroutine Complete");
FindAudioLength(source.clip);
}
#endregion
As stated before I can get the build to work with music, but it has to have been in the asset folder of the editor beforehand. (sidenote the current code is giving an index out of bounds error, was fiddling with the code and seem to have messed up something)
Your answer
Follow this Question
Related Questions
How to stop and re-start audio? 1 Answer
Can Not Play Disabled Audio Source 1 Answer
Second AudioClip won't play 0 Answers
Second AudioClip won't play 0 Answers
Take 2: Audio silent in Play Mode. 0 Answers