Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ironside_h · Jul 05, 2018 at 03:44 AM · 2d gameaudiosourceaudiocliprandomize

How can I play one music right after the other ends?

I've many songs that I want randomly play one of it everytime the game starts and then when a sound ends, continue playing and repeating my playlist with a fading between the musics... How can I do this? I was wondering maybe create a GameObject and just attach my 30 songs in it, but I don't think this is the best way to do it...

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 NoDumbQuestion · Jul 05, 2018 at 06:59 AM 0
Share

You gotta call update to check if song is ended.

https://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Jul 05, 2018 at 07:07 AM

Good day.

You need to check in the updte ifthe audiosource is still playing. When is not, you can plsy the next audio clip

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 LEDWORKS · Jul 05, 2018 at 01:12 PM 0
Share

This is the correct answer ^

avatar image MaxLohMusic · Feb 03 at 06:16 AM 0
Share

All these fail to function correctly when user alt tabs

avatar image
0

Answer by AkshayBisht · Jul 05, 2018 at 10:12 AM

If you want to add fading and other effects, i would recommend using Unity Audio Mixer.
Or you can do this..
adding two game objects to your scene
randomly adding music to their audio source
then decreasing volume of one while increasing volume of other gameobject's audio source.

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
avatar image
0

Answer by Vicente54 · Feb 03 at 09:07 AM

Looking for the same issue. Bumped into your thread. Thanks for creating it. Looking forward for solution.

MyBalanceNow

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
avatar image
0

Answer by MaxLohMusic · Feb 03 at 09:52 AM

The common solution most would recommend is to check isPlaying in the Update() method and play next clip if it's not playing. However there's a very annoying issue: Every time you alt-tab it will consider it "not playing" and start a new piece. So, in addition to that, use the Monobehaviour's OnApplicationFocus(bool b) method. Keep track of it with a variable like

  void OnApplicationFocus(bool b) {
         Debug.Log("application focus: " + b);
         isFocused = b;
     }

If isFocused is false, don't play the next clip.

  // Update is called once per frame
     void Update()
     {
         if(isFocused && !currentAudio.isPlaying)
             playNextMusic();
     }


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
avatar image
0

Answer by jdbenitez · Feb 03 at 11:20 AM

Solution one: launch your music in a corroutine and wait async for end;

 musicAudio.Play();
 yield return new WaitForSeconds(musicAudio.clip.lenght);
 nextMusic();

Solution two: launch yout music in a corroutine an wait async while is playing

 musicAudio.Play();
 yield return new WaitWhile(() => musicAudio.isPlaying );
 nextMusic();
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

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

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

Audio won't play sound 2 Answers

Multiple PlayOneShot bug 1 Answer

Loading Audio from Asset Bundles not working in Unity 5.2 2 Answers

AudioClips automatically fastforwarding at beginning of scenes 0 Answers

Why do AudioClips still play with no Audiolistener? 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