0
\$\begingroup\$

EDIT: I updated the code to what I have currently. I'm not sure why but, the editor and my platform complained when I put the WWW in a co-routine, it seems to load the audio clips to the object but I cannot play them. In a script attached to the object I wait until the Audio Clip is loaded before I try to play it.

So I am trying to record some Audio through the microphone and then save an AudioClip to the disk as a .wav file and also save the filepath as a playerpref and then later I can come back and load the string into a url but, for some reason the Audio Clip doesn't get loaded properly as I can see that there is an audio clip on my audio source in runtime but, it has no name and it has no sound on it. However, when I press play in the inspector I can hear my sound

I am using SavWav.cs to save my audio clips to .wav files.

Here is the url(filepath) I am sending to my load method:

string filename = string.Format(@"CapturedAudio{0}_n.wav", Time.time);
string filepath = Path.Combine(Application.dataPath, filename);

Here is the method I am using to get the AudioClip:

private void SpawnObject(string savedNames, Vector3 savedPositions, Quaternion savedRotations)
{

        GameObject cubeToSpawn = Instantiate(cube, savedPositions, savedRotations);
        WWW www = new WWW("file:///"+savedNames);
        cubeToSpawn.GetComponent<AudioSource>().clip = www.GetAudioClip(false, false, AudioType.WAV);

}

The obj is just a cube with an AudioSource attached to it that I instantiate prior to calling this method above.

The filepath works fine for saving but, for some reason it doesn't want to load the audio clip. Any help would be greatly appreciated.

\$\endgroup\$
  • \$\begingroup\$ That was a bug in my code but, I did fix that. My problem is that I can see that the audio clip is loaded onto my game object and I can play it in the inspector, it won't play in game even when I wait for the audio clip to be loaded. The audio clip appears to be empty in the fact that in the inspector it has no name and no sound information on it but I can hit play and hear my recording fine in the inspector. \$\endgroup\$ – Dtb49 Nov 22 '17 at 14:41
1
\$\begingroup\$

Well, after a little more debugging I found that I was calling Microphone.End before I was saving the clip and also because I had my StopRecording() function containing Microphone.End(deviceName) on a button it was referencing my device name as null so it was never ending the Microphone and I assume this is what was really causing all of the problems.

\$\endgroup\$
0
\$\begingroup\$

I had better luck saving to the resources folder then loading from there.

void SaveRecording(AudioSource aud) { SavWav.Save(Application.persistantDataPath + "/Resources/" + aud.clip.name, aud.clip); }

and then load

AudioClip _clip = Resources.Load(_name) as AudioClip;
    AudioSource _aud = GetComponent<AudioSource>();
    _aud.clip = _clip;
    _aud.Play();
\$\endgroup\$
  • \$\begingroup\$ Note that for use in release, you'll likely want to use Application.persistentDataPath, and load it using WWW. On some platforms Application.dataPath is not writeable in released builds. \$\endgroup\$ – DMGregory Nov 21 '17 at 22:25
  • \$\begingroup\$ As @DmGregory said, I have to use Application.persistentDataPath in the release I am only using dataPath for debugging in the Editor. \$\endgroup\$ – Dtb49 Nov 22 '17 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.