Question by
Killercrush · Jan 26, 2016 at 11:18 PM ·
errorwwwaudioclip
Error without text
I'm trying to play sound file, downloaded with WWW class. Here is the code:
using UnityEngine;
using System.Collections;
public class audio : MonoBehaviour
{
AudioSource source;
// Use this for initialization
void Start()
{
source = GetComponent<AudioSource>();
StartCoroutine(StartAudio());
}
IEnumerator StartAudio()
{
WWW audioLoader = new WWW("file://E:/Music/sample.wav");
AudioClip a_clip = audioLoader.audioClip;
while (!a_clip.isReadyToPlay)
yield return audioLoader;
source.clip = audioLoader.GetAudioClip(false);
source.Play();
}
}
When I run the project, sound don't play and I get error without text:
I have tried to delete all preferences and reinstall Unity, but it doesn't help. I'm really don't know what to do =(
error.png
(180.5 kB)
Comment
Best Answer
Answer by gjf · Jan 26, 2016 at 11:17 PM
that's indicative of unity not finding your URL.
this code works for reading from a web server or local file system. it plays any sound currently set up (to ensure the audio source is working properly...)
using UnityEngine;
using System;
using System.Collections;
public class Loader : MonoBehaviour
{
private AudioSource _audioSource;
private string _audioURL = "www.yourDomainHere.com/filename.wav";
//private string _audioURL = "file:///Users/yourNameHere/Dropbox/filename.wav";
//
public void Start()
{
_audioSource = GetComponent<AudioSource>();
if (_audioSource == null)
{
Debug.Log("No AudioSource component found!");
}
else
{
if (_audioSource.clip != null)
{
_audioSource.Play();
}
StartCoroutine(LoadAudioClip(_audioURL));
}
}
//
private IEnumerator LoadAudioClip(string audioURL)
{
Debug.Log("Loading clip from WWW");
var audioLoader = new WWW(audioURL);
var clip = audioLoader.audioClip;
yield return new WaitUntil(() => clip.loadState.Equals(AudioDataLoadState.Loaded));
if (clip.loadState.Equals(AudioDataLoadState.Loaded))
{
_audioSource.clip = audioLoader.GetAudioClip(false, false);
Debug.Log("Playing clip...");
_audioSource.Play();
}
}
}
Thank you for the answer! It looks like the problem was in my sample file itself, despite that it played when I set it directly in AudioSource (maybe Unity Editor converted it somehow, when I import the file in Assets).