- Home /
Question by
datamaru · May 29, 2015 at 05:46 AM ·
audioloadaudioclippersistentdatapath
How to play audio from Application.persistentDataPath?
I want to play the audio I saved to the persistent data path but i can't get it to work.
Here's my code :
public class monyetRecord : MonoBehaviour {
AudioSource monkeyAudio;
public string url;
public WWW audioFile;
public AudioClip monkeyClip;
void Awake()
{
}
// Use this for initialization
void Start () {
monkeyAudio = GetComponent<AudioSource> ();
url = "file://" + (Application.persistentDataPath);
audioFile = new WWW (url + "/testAudio.wav");
Debug.Log (url);
}
// Update is called once per frame
void Update () {
}
//for recording narration
public void RecordMonkeyVoice()
{
monkeyAudio.clip = Microphone.Start (null, true, 10, 44100);
}
public void StopRecord()
{
Microphone.End (null);
SavWav.Save("testAudio", monkeyAudio.clip);
}
public void PlayMonkeyVoice()
{
if (!monkeyAudio.isPlaying) {
monkeyAudio.clip = audioFile.GetAudioClip(false, false, AudioType.WAV);
monkeyAudio.Play ();
Debug.Log (audioFile.GetAudioClip (false, false));
}
}
}
I've been stuck at this problem for days. Please, any help will be appreciated
Comment
You found a solution ? Please let us know. Ty in advance
Answer by Marrt · Apr 13, 2018 at 12:54 AM
As of now this has worked for me, fullpath is the full path of the target file
private IEnumerator LoadFile( string fullpath ) {
print("LOADING CLIP "+fullpath);
if ( !System.IO.File.Exists( fullpath )){
print("DIDN'T EXIST: "+fullpath);
yield break;
}
AudioClip temp = null;
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip( "file://" + fullpath , AudioType.WAV))
{
yield return www.SendWebRequest();
if (www.isError){
Debug.Log(www.error);
}else{
temp = DownloadHandlerAudioClip.GetContent(www);
}
}
changeFunction.Invoke(temp);
}