Unity server reads the sound files

Use WWW function, in addition to download pictures, sounds can be downloaded, and download pictures similar to the method, the following is a simple example:

1. Place a sound file in the root directory of the site Alarm01.wav (this is the window system comes not find this format can be directly in the C disk search)

2. Add clipPath, audio, m_downloadClip property and DownloadSound () function in WebManager.cs script:


    #region 声音下载

    string clipPath = "http://127.0.0.1:8088/Alarm01.wav";

    protected AudioClip m_downloadClip;

    AudioSource audio;
    #endregion

 IEnumerator DownloadSound()
    {
        WWW www = new WWW(clipPath);

        yield return www;

        if (www.error != null)
        {
            m_info = www.error;
            yield return null;
        }

        m_downloadClip = www.GetAudioClip(true,true,AudioType.WAV);
        audio.clip = m_downloadClip;
        audio.Play();
    }

3. Add a script component mounted AudioSouce game object body

4. Awake performed in DownloadSound function () function and locate assembly Audiosource

    private void Awake()
    {
        audio = GetComponent<AudioSource>();
        StartCoroutine(DownloadSound());
    }

note:

Here www.GetAudioClip (true, true, AudioType.WAV) last a need to modify the format is not supported directly read MP3 audio files in a window platform, we usually an MP3 file will be placed in the packing process engineering Unity, wav the format is usually recorded files.

Published 62 original articles · won praise 5 · Views 3925

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/103054040