Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c# - Why Audio not works in my WebGl app when mp3 size is bigger than 3 megabyte?

I've problem/bug trying to load an audio file from an URL using UnityWebRequestMultimedia.GetAudioClip. This is my code:

// I run a coroutine from my main program
StartCoroutine(LoadBackgroundMusic(0, false));


public IEnumerator LoadBackgroundMusic(int frameID, bool playMusicAfterDownload)
{

    string webservice_url = gsStaticClassManager.WEBSERVICE_URL + "/GetAudioFileName";
    string filename = "";
    string audioURL = gsStaticClassManager.GALLERY_REPOSITORY_PATH + "/" + GalleryGUID + "/";

    WWWForm form = new WWWForm();

    form.AddField("galleryGUID", GalleryGUID);
    form.AddField("frameID", frameID.ToString());

    // Retrieve audio filename using a web service i've created
    WWW www = new WWW(webservice_url, form);
    yield return www;
    if (!string.IsNullOrEmpty(www.error))
    {
        Debug.Log(www.error);
        LogAdd(www.error.ToString(), true);
    }

    filename = www.text;  // get audio FileName
    audioURL = audioURL + www.text; // audioURL is a fixed value + filename

    
    AudioType audioType = AudioType.UNKNOWN;
    if (System.IO.Path.GetExtension(filename).ToLower() == ".mp3")
        audioType = AudioType.MPEG;
    else if (System.IO.Path.GetExtension(filename).ToLower() == ".ogg")
        audioType = AudioType.OGGVORBIS;

    using (UnityWebRequest www2 = UnityWebRequestMultimedia.GetAudioClip(audioURL, audioType))
    {
        yield return www2.SendWebRequest();

        if (www2.isHttpError)
        {
            Debug.Log(www2.error);
            LogAdd(www.error, true);
        }
        else
        {
            AudioClip downloadedClip = DownloadHandlerAudioClip.GetContent(www2);
            _audio.clip = downloadedClip;
            if (playMusicAfterDownload)
                PlayBackgroundMusic(); // here Play Audio music
        }
    }
}

public void PlayBackgroundMusic()
{
    if (_audio.clip != null && _audio.isPlaying == false)
        _audio.Play();
    else
    {
        Debug.Log("Background music not present!");
        if (_audio.clip != null)
            Debug.Log("_audio value:" + _audio.clip.name);
        else
            Debug.Log("_audio is null");
    }
    _audio.loop = true;
}

When my file is less then 2/3 megabyte it works, bigger no (I tried with different length files, all encoded with same software and options (Audacity and mp3)). In my opinion it isn't a webserver problem. Could be a problem using two call to 'www' in the same routine ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...