Adam H
Adam H

Reputation: 1573

Unity3D stream audio file from disk

I'm building an audio player that needs to be able to play files from disk which aren't available at build time (so users can put their own music in). OGG/WAV files are fine and I need to support both Windows and OSX. I've tried using the WWW class as such:

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

public class LoadAudio : MonoBehaviour
{
    AudioSource audioSrc;

    // Use this for initialization
    void Start()
    {
        if(audioSrc == null) audioSrc = new AudioSource();
        StartCoroutine(LoadTrack(Path.Combine(Application.streamingAssetsPath, "Track.ogg")));
    }

    IEnumerator LoadTrack(string filename)
    {
        var www = new WWW(filename);

        while(www.progress < 0.2)
        {
            Debug.LogFormat("Progress loading {0}: {1}", filename, www.progress);
            yield return new WaitForSeconds(0.1f);
        }

        var clip = www.GetAudioClip(false, true, AudioType.OGGVORBIS);

        audioSrc.clip = clip;
        audioSrc.Play();
    }

    // Update is called once per frame
    void Update()
    {

    }
}

However, when I run this my debug log message prints out that there is 0 progress a few times, then I get an error that can't be seen in the Console. In the Editor log, I can see this error:

Error: Cannot create FMOD::Sound instance for resource D􉂬 (Operation could not be performed because specified sound/DSP connection is not ready. )
UnityEngine.WWW:GetAudioClipInternal(Boolean, Boolean, Boolean, AudioType)
UnityEngine.WWW:GetAudioClip(Boolean, Boolean, AudioType) (at C:\buildslave\unity\build\artifacts\generated\common\runtime\UtilsBindings.gen.cs:334)
<LoadTrack>c__Iterator0:MoveNext() (at Assets\LoadAudio.cs:27)

This error looks similar to the one raised in this Unity bug report, however that was supposedly fixed in version 5.3.0 and I'm using 5.3.4.

Assuming this isn't a bug in Unity, what am I doing wrong? If this is a bug, then does anyone know a workaround?

EDIT: I submitted a bug report to Unity and they confirmed that they have reproduced the bug, but it may be a long time before it is fixed. As such, I would appreciate a way to work around this issue.

Upvotes: 2

Views: 3627

Answers (2)

Adam H
Adam H

Reputation: 1573

Adding file:// to the start of the filepath has solved this.

Upvotes: 3

Programmer
Programmer

Reputation: 125315

I looks like Track.ogg is NOT fully downloaded before you accessed the audio file. Use isDone to make sure that the audio is fully loaded and use yield return null instead of WaitForSeconds(0.1f) to wait without freezing. www.progress < 0.2 is a bad way of doing this.

AudioSource audioSrc;

    // Use this for initialization
    void Start()
    {
        if(audioSrc == null) audioSrc = new AudioSource();
        StartCoroutine(LoadTrack(Path.Combine(Application.streamingAssetsPath, "Track.ogg")));
    }

    IEnumerator LoadTrack(string filename)
    {
        var www = new WWW(filename);

        //Wait for file finish loading
        while(!www.isDone)
        {
            Debug.LogFormat("Progress loading {0}: {1}", filename, www.progress);
            yield return null;
        }

        var clip = www.GetAudioClip(false, true, AudioType.OGGVORBIS);

        audioSrc.clip = clip;
        audioSrc.Play();
    }

    // Update is called once per frame
    void Update()
    {

    }

Not tested but that should do it.

Upvotes: 1

Related Questions