Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Itsproinc · Jul 09, 2013 at 06:48 AM · changemusic

Change to another song when song is finish playing

Hello, is there is a way to change another music or tune? ie. when music 1 finish playing a song then music 2 plays then 3 then 4 and so on? i have search this everywhere but i couldn't locate it

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

2 Replies

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

Answer by AlucardJay · Jul 09, 2013 at 07:03 AM

Ok, so this one can be achieved by using the length of the audio clip and Invoke.

  • http://docs.unity3d.com/Documentation/ScriptReference/AudioClip-length.html

  • http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html

So the pattern would be : Play the audioClip, then Invoke a function at a time delay of audio.clip.length, that function assigns a new audioClip and repeats the process. Example pseudocode

 audio.clip = clip1;
 audio.Play();
 Invoke( "PlayNextTrack", audio.clip.length );

 // function PlayNextTrack
 audio.Stop(); // just in case
 audio.clip = clip2; // this would actually be choosing the next song in an array, after checking if the index is out of range then going back to the first track in the array
 audio.Play();
 Invoke( "PlayNextTrack", audio.clip.length );
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 zakirshikhli · May 17 at 08:45 PM 0
Share

Here is exact an code from my project with Invoke method.

  private void Start()
     {
             AudioListener.volume = PlayerPrefs.GetFloat("volumeSound", 1.0f);    
             music.ignoreListenerVolume = true;
             music.volume = PlayerPrefs.GetFloat("volumeMusic", 1.0f);
             if (music.volume > 0.0f) 
             {
                 PlayMusic();
             }
     }
       void PlayMusic()
         {
             music.clip = musics[Random.Range(0, musics.Length - 1)];
             music.Play();    
             Invoke(nameof(PlayMusic), music.clip.length);
         }
 
avatar image
5

Answer by flamy · Jul 09, 2013 at 07:16 AM

  public string[] _clipNames;     //Keep all audio files inside resources folder  and give the value of path alone in this varible through editor.
  int i=0;
 
  void Start()
  {
     StartAudio();   // start first time without Delay
  }
 
  void StartAudio()
  {
     audio.clip = Resources.Load (_clipNames[i]) as AudioClip;
     audio.Play();
 
     i++;
     if(i>=_clipNames.Length)
        i=0;
     Invoke("StartAudio",audio.clip.Length+0.5f);    //0.5f is the delay given after a song is over.
 
  }
 

Ok here these are the thinks that has to be noted. First I have used _clipNames array to save path of the audio clips that are kept in resources folder. I could have attached all the clips to the Editor by declaring AudioClip array instead of string but it doing that would make the clips stay in memory until the script instance is destroyed.. For more info on Resources folder check this page.

Next I have used Invoke function... with the delay as the length the currently playing audio clip. This way the next audio will be changed automatically once the audio clip is over.

Hope this is clear.

Comment
Add comment · Show 5 · 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 AlucardJay · Jul 09, 2013 at 07:23 AM 0
Share

I'm a little disappointed you have given the same answer, but also provided code. Who cares, right, I don't need the karma. But don't you think there's enough there in my answer to get the ball rolling? And am I justified in feeling a little plagiarized?

Also, any response from the OP will indicate their level of knowledge.

Sure, I write code for people too, but a good guide is : If they provide code, fix and/or provide code; If they provide an example, give an example.

avatar image AlucardJay · Jul 09, 2013 at 07:27 AM 0
Share

Also, your Invoke is correct. The name of the method as a string :

 function Invoke (methodName : String, time : float) : void 
avatar image flamy · Jul 09, 2013 at 07:33 AM 2
Share

i was typing it for ages... and completed it jus now... didnt knew you have answered already =/

Changing the invoke..

avatar image AlucardJay · Jul 09, 2013 at 07:37 AM 1
Share

Ok, no problem then. It was a simultaneous post. Sorry if the above comments offended you, I totally understand now.

Edit : It was a simultaneous post (not duplicate like I wrote first). You have always been one of the good guys (that's why I was initially surprised), UA needs people like you !

avatar image flamy · Jul 09, 2013 at 07:41 AM 0
Share

it is not a issue... I dont take any offense :)

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I create an effective spawn point script for multiple scenes and change music each scene? 1 Answer

Making all instances of an object glow, and not get affected by post-processing 2 Answers

Non-uniform Scaling Performance Issues. Need advice. 1 Answer

Change GUI size 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