Search Unity

Streaming audio bug ?

Discussion in 'Audio & Video' started by AbgaryanFX, Mar 14, 2017.

  1. AbgaryanFX

    AbgaryanFX

    Joined:
    Jan 9, 2010
    Posts:
    167
    I'm trying to force Unity to play audio mp3 file while it's streaiming, but it doesn't play until the end of download.
    Here is a strange part: If I press right-click and open any context menu in UNITY EDITOR, it will play audio while downloading the file.

    Am I doing something wrong or it's a unity bug ?

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class AudioPlayer : MonoBehaviour
    6. {
    7.     string urlMp3 = "";
    8.  
    9.     public AudioSource source;
    10.  
    11.     private void Start()
    12.     {
    13.         source = GetComponent<AudioSource>();
    14.         InitButton(urlMp3);
    15.     }
    16.  
    17.     public void InitButton(string url)
    18.     {
    19.         urlMp3 = url;
    20.         progress = 0;
    21.         source.time = 0f;
    22.         StartCoroutine(StartDownload());
    23.     }
    24.     public float progress = 0;
    25.  
    26.     IEnumerator StartDownload()
    27.     {
    28.         WWW www = new WWW(urlMp3);
    29.         print("start");
    30.         while (!www.isDone)
    31.         {
    32.             progress = www.progress;
    33.  
    34.             source.clip = www.GetAudioClip(false, true);
    35.             //source.clip.name = "Abo";
    36.             if(!source.isPlaying) source.Play();
    37.             print("try to play " + source.isVirtual); // must not be virtual
    38.             yield return null;
    39.         }
    40.  
    41.         if (null != www.GetAudioClip(false, true, AudioType.MPEG))
    42.         {
    43.             source.clip = www.GetAudioClip(false, true);
    44.             print("play after download");
    45.  
    46.             source.Play();
    47.         }
    48.      
    49.         source.time = 0f;
    50.      
    51.      
    52.     }
    53.  
    54. }
    55.  
     
    Last edited: Mar 19, 2017