audio.play does not work

I have used WWW.GetAudioClip() to import a song at runtime into my game. The import works perfectly, but when I try and play my sound, it does not play.

public AudioSource adio;

	public void Start()
	{
		WWW www = new WWW("file://C:/sound.ogg");

		AudioClip a = www.GetAudioClip(true, false, AudioType.OGGVORBIS);
		adio.clip = a;

		adio.Play();

        Debug.Log (adio.isPlaying);
	}

The debug says that the sound is in fact not playing, but in the AudioSource component, I can clearly see that there is a clip variable, and I can even manually listen to it if I click on it and listen through the editor, so it’s not a problem with the import.

What could possibly be causing the audio to not play? I have also tried adding a delay and using PlayOneShot(). playOnAwake makes no difference regardless of it’s setting either.

The problem with trying to play an audio clip with www is that if you do not wait until the audio has not completely loaded before trying to play the clip it will not properly work. Try this class out

using UnityEngine;
using System;
using System.IO;
using System.Collections;

public class PlayAudioFile : MonoBehaviour
{
    public AudioSource adio;
    
    public string filePath = "file://C:/sound.ogg";

    private AudioClip audioClip;

    public void Start()
    {
        StartCoroutine(LoadAndPlaySound(filePath));
    }

    IEnumerator LoadAndPlaySound(string filePath)
    {
        WWW www = new WWW(filePath);

        while(!www.isDone)
            yield return null;

        if ((www.bytesDownloaded > 0) && (www.audioClip != null))
        {
            // audioClip = www.GetAudioClip(false);
            audioClip = www.GetAudioClip(true, false, AudioType.OGGVORBIS);

            while (!audioClip.isReadyToPlay)
                yield return www;
            audioClip.name = Path.GetFileNameWithoutExtension(filePath);

            PlayAudioClip();
        }

        yield return null;
    }
    
    public void PlayAudioClip()
    {
        if ((audioClip != null) && (audioClip.isReadyToPlay))
        {
            adio.PlayOneShot(audioClip);
        }
    }
}