- Home /
How to import wav file trough script from Project folder?
I have app when i press button it should import .wav file and on other button it plays that wav file. it needs to be specialy song with for example "beat.wav". i tried with WWW but cant figure out how to get to project folder, no assets folder but project!
Im making a little music game, and i made a script that saves wav file of microphone.Record()... After button for save is pressed it saves it in project folder, not in assets folder... And now i need a way to set that as audio clip for audio source attached to one of my game Objects.
Are you using unity's implementation of microphone recording? https://docs.unity3d.com/ScriptReference/$$anonymous$$icrophone.html
If you do, function $$anonymous$$icrophone.Start return AudioClip object which you can use with an AudioSource
Thanks, but its not all for mic, i have and sound wav maker something, that cant be played like that...
Here is a script that I made hopefully it helps out
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class AudioAccessor: $$anonymous$$onoBehaviour
{
//In this case I never looked for the game folder ins$$anonymous$$d i checked for the audio files in "my documents"
private string GamePath;
private DirectoryInfo GameDirectory;
//Audio info
private DirectoryInfo AudioDirectory;
private string AudioPath;
private FileInfo[] AudioFilesFound;
private AudioClip clip;
public List<AudioClip> LoadedAudioWeapons = new List<AudioClip>();
void Start()
{
CheckFoldersAndLoad$$anonymous$$odObjects();
}
public void CheckFoldersAndLoad$$anonymous$$odObjects ()
{
if(File.Exists(System.Environment.SpecialFolder.$$anonymous$$yDocuments + "\\TheGamesName") == false)//if there was nothing found in my documents then create them
{
CreateFolder();
}
else //Otherwise if the stuff exists just the the path
{
GameDirectory = System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.$$anonymous$$yDocuments) + "\\TheGamesName");
GamePath = GameDirectory.FullName.ToString();
}
//Load all objects from folders here
GetAudioFromFolder();
}
//Creation of files
private void CreateFolder()
{
GameDirectory = System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.$$anonymous$$yDocuments) + "\\TheGamesName");
GamePath = GameDirectory.FullName.ToString();
}
private void GetAudioFromFolder()
{
AudioFilesFound = GameDirectory.GetFiles("*.ogg*");//Gets all files with the format ".ogg"
for (int k = 0; k < AudioFilesFound.Length; k++)
{
StartCoroutine(LoadAudioWeapons(AudioFilesFound[k].ToString()));
}
}
private IEnumerator LoadAudioWeapons(string fileName)
{
WWW www = new WWW("File:///" + fileName);
yield return www;
if(Path.GetExtension(fileName).ToUpper() == ".OGG")
clip = www.GetAudioClip(true, false, AudioType.OGGVORBIS);
while (clip.loadState == AudioDataLoadState.Loading)
yield return www;
clip.name = Path.GetFileName(fileName);
LoadedAudioWeapons.Add(clip);
}
}
It accesses the files from my documents, you might want yo change the file path if you want to access your project folder, i think its Application.dataPath but I'm to lazy to look XD
Another note, I'm not to sure if you can import .wav so you just going to have to double check as I originally wanted to import .mp3 but unity wouldn't allow it.
Your answer
Follow this Question
Related Questions
UnityWebRequest hangs on isDone but not always 1 Answer
Is it possible to access the speaker in the DualShock 4 controller without the PS4 kit? 1 Answer
Problem with audio 1 Answer
Why does loading music files at runtime in android using www class takes a lot of time? 0 Answers
How to do a FFT in Unity? 3 Answers