Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Streaming audio: zero length and no isPlaying?

Discussion in 'Editor & General Support' started by Martin-Schultz, Mar 9, 2008.

  1. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    I'm trying to let the first song for our game be streamed and meanwhile load he rest from the web so that after the first song is over, all other songs are availble to be played too.

    For the loading of the Ogg tracks I use the excellent example that comes in the script docs of the www.oggVorbis documentation.

    Now, for the first track I use the following code:

    Code (csharp):
    1.  
    2. function DownloadAndPlayFirstTrack()
    3. {
    4.     www = new WWW("http path to some ogg file");
    5.     audio.clip = [url]www.audioClip;[/url]
    6.     // Check when first song is ready to play, then load also all others
    7.     Invoke("FirstSongStartup", 0.5);
    8. }
    9.  
    Then in a service function I check whether the file is already streamed enough to get started:


    Code (csharp):
    1. function FirstSongStartup() {
    2.  
    3.     if (!audio.isPlaying  audio.clip.isReadyToPlay)
    4.     {
    5.         audio.Play();
    6.         Invoke("FirstSongEnd", 0.2);
    7.         print("first song length: " + audio.clip.length); // returns always zero!
    8.     } else {
    9.         Invoke("FirstSongStartup", 0.5);   
    10.     }
    11. }
    In Here I check whether the song is still beeing played or not:

    Code (csharp):
    1. function FirstSongEnd() {
    2.     // check every 10 seconds if the song is still playing.
    3.     Invoke("FirstSongEnd", 10);
    4.    
    5.     print("length: " + audio.clip.length + ", isPlaying: " + audio.isPlaying + ", loop: " + audio.loop);
    6. }
    Now my problem is, that the function audio.clip.length always returns zero, also when the song is fully loaded. Additionally the function audio.clip.isPlaying is always returning true after the first "play()" call and never gets to false after the song has finished (loop is off!).

    Does anyone know why or a way around? This looks like a bug in Unity to me as I would expect the song length be available after it has been fully loaded and I would expect the audio.clip.isPlaying beeing false when the track has reached the end. Any clue?

    Thanks,
    Martin
     
  2. Deleted User

    Deleted User

    Guest

    I reported the same problem with AudioSource.isPlaying via the bug reporter just a few days ago and got confirmation that it is a bug and will be patched. In my case, I was doing the test in an Update() function, similar to the sample code in the WWW.audioClip doc (overall I was taking an existing version of the multiple-random-download-and-play script shown in the doc for WWW.oggVorbis and converting it to stream with AudioClip.isReadyToPlay)

    Sam Kalman sent me the following workaround. I haven't tried it, so I don't know if it would exhibit the same zero-length problem with audio.clip.length


    Code (csharp):
    1.  
    2. var path:String;
    3.  
    4.  
    5. function Start ()
    6. {
    7.     StartCoroutine ("DownloadAudio");
    8. }
    9.  
    10. function DownloadAudio()
    11. {
    12.     var www = new WWW (path);
    13.     yield www;
    14.     audio.clip=www.audioClip;
    15.     PlayLoopingAudio();
    16. }
    17.  
    18. function PlayLoopingAudio ()
    19. {
    20.     while (true)
    21.     {
    22.         audio.Play();
    23.         yield WaitForSeconds (audio.clip.length);
    24.     }
    25. }
    26.  
    27.  
    28. @script RequireComponent (AudioSource)
    29.  
    [/code]
     
  3. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Ah, thanks for the hint. Interesting, he put it into a Coroutine function. Unfortunately didn't work for me, but thanks anyway. And good to hear I'm not the only one stumbled over this. I changed my code now that the first song is included in the game project and the rest is beeing downloaded meanwhile.
     
  4. Superamok

    Superamok

    Joined:
    Feb 10, 2008
    Posts:
    12
    Bump!

    The problemed described here seems not to be fixed yet. At least I get the same in my current project:

    Cant use audio.clip.length and audio.clip.isPlaying when streaming sound.

    Is there an eta for a fix?

    Thanks
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    The last version of Unity was released in January. I believe all current bugs are scheduled to be fixed in version 2.1. Don't know when that's going to be released but that's what you should look out for.
     
  6. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    Im running Unity 2.1 and I have the same problem. I do:

    Code (csharp):
    1. IEnumerator LoadAudio( string path )
    2. {
    3.     // start download //
    4.     WWW www = new WWW( "file://" + path );
    5.    
    6.     // wait for the file to load //
    7.     yield return www;
    8.        
    9.     //print audio clip length //
    10.     Debug.Log( "www.audioClip.length = " + [url]www.audioClip.length[/url] );
    11. }
    The console prints "audioClip.length = 0" :-(

    Can anyone please confirm that the bug is NOT fixed in Unity 2.1 ... or let me know if Im missing something?
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  8. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    Thanks Jessy, www.oggVorbis does the trick. Streaming would be an advantage though. I hope this will be fixed in a future update.
     
  9. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    We are using Unity 3.4.2 now, and the audio.isPlaying problem still exists, with one difference audio.clip.length does not return 0 but, some numbers depending on the size of audio file.
     
  10. kundo

    kundo

    Joined:
    Jul 19, 2010
    Posts:
    25
    Hi ! We are using 3.5.0 and the bug is still there.
    We have a lot of sounds and using streaming is very important.
    In this code audio.clip.isPlaying never returns to false.

    www = new WWW(url);

    if (www.error == null)
    {
    audio.clip = www.GetAudioClip(false,true);
    while ( !audio.clip.isReadyToPlay ) yield return 0;
    audio.Play();
    }

    Anyone found a solution ?
    Thanks !
     
  11. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    341
  12. Conferno

    Conferno

    Joined:
    Feb 27, 2014
    Posts:
    49