Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 26, 2017 at 06:46 PM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kab_6182 · Jun 22, 2017 at 12:44 AM · coroutineaudiosourceloopingaudioplay

How to make a loop yield for the length of audio,how to make a loop wait for audio lengh

I have multiple audio sources through out the scene. I created a coroutine so the audio loops through all the audio sources.

        public List<AudioSource> sourcesInScene; //assign these in the inspector
         private List<int> indexCountA = new List<int>();
           public void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         ResetIndexCountA(sourcesInScene.Count);
         StartCoroutine(RandomPlayBack());
     }
             private void ResetIndexCountA(int number)
 {
     indexCountA.Clear();
     for (int i = 0; i < number; i++)
     {
         indexCountA.Add(i);
     }
      }

               IEnumerator RandomPlayBack()
 {
     //we're using an ienumerator so we can "wait" between loops.
     while (indexCountA.Count != 0)
     {
         for (int x = 0; x < sourcesInScene.Count; x++)
         {
             var i = Random.Range(0, indexCountA.Count);
             sourcesInScene[indexCountA[i]].Play();
             indexCountA.RemoveAt(i);

             yield return new WaitForSeconds(1f);
         }

         yield return null;
     }
 }

My question is on the line where I wrote "yield retrun new WaitForSeconds(1f);" instead of me specifing the yield time such as 1f how can I make it so it loops as soon as the previous audio is finished playing. Remember am not just playing audio clips, where i could yield using audioclip.length. I am working with multiple audio sources.

,

Comment
Add comment · Show 2
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 nt314p · Jun 22, 2017 at 12:56 AM 0
Share

Can you mix all of the audio into one audio track and then loop that one audio clip?

avatar image hexagonius · Jun 22, 2017 at 07:01 AM 0
Share

An AudioSource has a time attribute. So whenever you start an additional source just subtract each time from each clip length, sort them by the longest time and yield that.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Eno-Khaon · Jun 22, 2017 at 03:34 AM

Maybe I'm misunderstanding something, but can you not still use AudioClip.length? Specifically...

 sourcesInScene[indexCountA[i]].clip.length

... should hook to the AudioClip assigned to the AudioSource that you're instructing to play. In that case, you can simply swap two lines and you should be good to go.

 sourcesInScene[indexCountA[i]].Play();
 yield return new WaitForSeconds(sourcesInScene[indexCountA[i]].clip.length);
 indexCountA.RemoveAt(i);
Comment
Add comment · Show 2 · 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 kab_6182 · Jun 22, 2017 at 01:23 PM 0
Share

thank you man i did not know i could use length attribute for audiosources

avatar image aldonaletto kab_6182 · Jun 23, 2017 at 12:35 PM 0
Share

I've converted this to an answer - please accept this answer if it solves your problem (it was posted before $$anonymous$$e).

avatar image
0

Answer by aldonaletto · Jun 22, 2017 at 08:05 PM

Why can't you use the clip length? Save it in a variable and set the delay in the yield:

  for (int x = 0; x < sourcesInScene.Count; x++) {
      var i = Random.Range(0, indexCountA.Count); 
      AudioSource aud = sourcesInScene[indexCountA[i]]; // get a reference to the AudioSource [i]
      float duration = aud.clip.length; // get its duration in a variable
      aud.Play(); // start playing
      indexCountA.RemoveAt(i); // remove it from the list
      yield return new WaitForSeconds(duration); // wait the clip finish playing
 }


Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

75 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 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 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 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

How to start a courine with input from keyboard 1 Answer

Add Audio Source to a Prefab 2 Answers

How to play multiple audioclips from the same object? 4 Answers

How Do I Get This To Play Sound? 0 Answers

Fade, Then Stop all other audiosources attached to gameobject? 1 Answer


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