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
0
Question by Mossey · Oct 28, 2013 at 01:32 PM · audiosourcequeue

Queueing audio

Let's say I have 2-3 songs that I want to play one after another. I am very close to implementing, but I have a little problem when switching those. They are of type AudioSource.

 function controlMusic()
 {    
     // Toggle music
     if(Input.GetKeyDown("m"))
     {
         music1.mute = !(music1.mute);
         music2.mute = !(music2.mute);
     }
     
     if (!(music1.isPlaying || music2.isPlaying))
     {
         switch (currentMusic)
         {
             case 1:
             music1.Play();
             break;
             
             case 2:
             music2.Play();
             break;
         
         }
         currentMusic++;
         if (currentMusic > 2) currentMusic = 1;
     }
 }

My problem is that this code doesn't work normally. However if I go to inspector during runtime and set music1 inactive(by clicking the tick), then it performs the switch.

Comment
Add comment · Show 1
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 vexe · Oct 29, 2013 at 12:33 PM 0
Share

@$$anonymous$$ossey how did it go? Please keep your answerers updated of your status.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Oct 28, 2013 at 03:42 PM

Ok let's get it back to the beginning.

 public AudioSource [] audioSource;
 int index = 0;
 void Start()
 {
    audio.clip = audioSource[index];
    StartCoroutine(PlaySong());
 }
 
 IEnumerator PlaySong()
 {
     while(true)
     {
         if(!audio.isPlaying)
         {
             if(++index == audioSource.Length)index = 0;
             audio.clip = audioSource[index];
             audio.Play();
         }
         yield return null;
     }
 }

Try this since I have not....

What it does now: First you have the array and an index. In the Start, you set the clip to be the first one. Then you start a coroutine that will play non-stop.

The coroutine is an infinite while loop which gets out every frame. In between, it checks whether the music is playing. If the music stops because we reached the end of the clip, the index is increased and checked against the length of the array. New clip is passed and play.

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 Mossey · Oct 28, 2013 at 03:52 PM 0
Share

I am getting indexOutOfBound as well as my game crashes probably due to infinite loop. The music#1 plays.

avatar image fafase · Oct 28, 2013 at 04:36 PM 0
Share

$$anonymous$$y mistake, the > is ==.Infinite loop won't make the crash since it is getting out when hitting the yield.

avatar image
0

Answer by tw1st3d · Oct 28, 2013 at 01:38 PM

Sorry that this is in C#, but I was a bit in a rush to get this done. It's explained fully, so if you don't know C# it won't be hard to understand.

 /* Save under MusicRandomizer.cs */
 using UnityEngine;
 using System.Collections;
 
 public class MusicRandomizer : MonoBehavior
 {
     private int curAudio = 0;
     // Current audio file selected, out of an array.
     // Inside of a C# array, the first element is listed as [0],
     // so we set curMusic to 0
     
     public AudioClip[] clips; // Define in editor
     
     private void Update()
     {
     
         if(!gameObject.audio.clip.isPlaying)
         // If no audio is playing
         {
         
             if(curMusic > clips.Length)
             {
                 curAudio = 0;
                 // If curAudio is higher than your total clips, set back to 0
             }
             
             gameObject.audio.clip = clips[curAudio];
             // Set game objects audio clip
             
             gameObject.audio.clip.Play();
             // Play game objects audio clip
             
             curAudio++;
             // Increment curAudio
         }
         
     }
     
 }
Comment
Add comment · Show 9 · 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 Mossey · Oct 28, 2013 at 02:02 PM 0
Share

I have a slight problem with this:

  public AudioClip[] clips; // Define in editor

How do I do this?

avatar image Statement · Oct 28, 2013 at 02:10 PM 0
Share
 var clips : AudioClip[];
avatar image Mossey · Oct 28, 2013 at 02:13 PM 0
Share

That's not what I am looking for Statement, sorry. I am implementing the C# version. It says that I have to define the array of AudioClips in the editor, but I don't know how to do it.

avatar image fafase · Oct 28, 2013 at 02:17 PM 0
Share

The declaration will give an array of size 0. Give size and free slot will show up. Drag and drop in those slots. This is it.

Unity will do the memory allocation.

avatar image Mossey · Oct 28, 2013 at 02:26 PM 0
Share

I am receiving errors on the lines:

 if(!gameObject.audio.clip.isPlaying)

and

 gameObject.audio.clip.Play();

They both say that UnityEngine.AudioClip does not contain a definition for those methods.

Show more comments

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

18 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

Related Questions

Rendering order of objects in different depths not correct. 1 Answer

Problem with .isPlaying 0 Answers

Audio.PlayOneShot problem 3 Answers

Problem with Unity 4.1 - AudioSource.ignoreListenerPause not working? 1 Answer

Android Audio Delay 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