Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Julien-Lynge · Nov 15, 2013 at 08:20 PM · audiocoroutinewww

Trouble streaming audio from WWW: plays only part of file

I'm trying to figure out how to stream in audio from a website using WWW. I've read through the documentation and an example or two I found online, and I've come up with the code below.

Unfortunately, it seems that no matter what I try, I can't get the file to play correctly. In a nutshell, what I'm doing is:

  • Start a download

  • Wait for the download to reach X percent complete

  • Extract the clip with GetAudioClip, which is supposed to give you a streaming clip

  • Add to an Audiosource and play

I'm seeing very strange behaviour, and I'm trying to figure out if it's a bug or if I'm misunderstanding something. The behaviour is inconsistent: if I try to buffer the clip to 25% complete I see one thing, if I try to buffer to 90% I see another thing, and neither is right.

  • If I set the buffer low, say 10% or 25% (www.progress < 0.25f), the audio only plays about a half second, pops a couple times, and dies.

  • If I set the buffer high, say 55-90%, it only plays the a second of audio in the middle or end (of a 4 second clip).

I'm hosting a random sample clip here that I'm testing against: http://earth-data.org/arribba.wav.

Is there a different way I should be doing this? Any best practices or samples? Or is what I'm seeing a Unity bug?

 private IEnumerator playAudio(string audioFile)
 {
     //set up a 2D audiosource to play the clip
     AudioSource audioSource = gameObject.AddComponent<AudioSource>();
     audioSource.playOnAwake = false;

     WWW download = new WWW(audioFile);

     //wait for the download to build up a buffer
     while (download.progress < 0.55f)
         yield return null;

     //Get a reference to the audio clip - this should allow us to stream it
     AudioClip clip = download.GetAudioClip(false, true);

     //just in case the isReadyToPlay bit hasn't been flipped
     Debug.Log("Set up audiosource.  Clip ready: " + clip.isReadyToPlay);
     while (!clip.isReadyToPlay)
         yield return null;

     audioSource.clip = clip;

     //start playing when the clip is ready
     audioSource.Play();
     Debug.Log("Playing audio: " + audioSource.isPlaying);

     //wait for the audio to finish
     while (audioSource.isPlaying || audioSource.timeSamples < clip.samples)
         yield return null;

     Debug.Log("Finished playing the audio clip.  Cleaning up");
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Nov 15, 2013 at 08:24 PM 0
Share

One other note: The last debug message is never reached. Whatever is happening appears to be breaking the audioSource so that it never finishes.

avatar image AlucardJay · Dec 11, 2013 at 08:39 PM 1
Share

Upvoted, you really do not deserve 2 negative votes for this (and for all your contributions to the community).

avatar image Julien-Lynge · Dec 13, 2013 at 04:39 PM 0
Share

Thanks @alucardj! I actually had some malicious downvoting recently - someone went through and downvoted the first few pages of my questions and answers twice. It's no big deal - it barely made a dent in my karma. I'm talking with the support staff and they're looking into it. They've been very helpful.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Julien-Lynge · Dec 11, 2013 at 06:44 PM

I have filed two bugs with Unity after further testing. One relates to trying to stream audio, and the other is for inconsistent behaviour between Play() and PlayOneShot().

The first bug was accepted, and the second is a duplicate of a known bug. In the meantime I've simply disabled streaming and wait for the full audio to download.

See bug 575799 in the issue tracker.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CostelloNicho · Mar 14, 2014 at 08:44 PM 0
Share

@Julien.Lynge ,

I've also defaulted back to not strea$$anonymous$$g an audio clip but downloading the full clip and waiting till it is ready to play. I'm having an issue where the length of the clip is not correct. It cuts 1-2 seconds off of the clip. This is a huge problem for me as I'm sending messages between users. Have you experienced this type of behavior? ( Unity version 4.3.2.f1 )

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW class implemented with threads? 1 Answer

An object reference is required to access non-static member 1 Answer

WWW, RESTful Service, and Threading 1 Answer

How do i stream audio? 0 Answers

Changing multiple UI.Image source image downloaded via WWW class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges