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 /
avatar image
0
Question by DemSec · Jun 22, 2016 at 06:00 AM · c#audiosourceaudioclipaudio.playoneshotaudio.play

Multiple gapless sound loops from one AudioSource (C#)

I have seen many questions about using multiple AudioSources but in my case, I want to use just one AudioSource, and one script controlling it. I have assigned 3 sound loops and I want to loop them without gaps all at once from one AudioSource, as well as stop and play individual sound loop with different individual volumes. Here's where I am right now:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class Sound : MonoBehaviour {
 
     AudioSource audioSource;
     public AudioClip audioClip1;
     public AudioClip audioClip2;
     public AudioClip audioClip3;
     [Range (0, 1)] public float audioClipVolume1;
     [Range (0, 1)] public float audioClipVolume2;
     [Range (0, 1)] public float audioClipVolume3;
 
     void Start () {
         audioSource = GetComponent<AudioSource> ();
         audioSource.loop = true;
     }
 
     void Update () {
         //How do I loop all 3 audio clips without gaps?
 
         //Tried using PlayOneShot
         //Checking if it is playing and playing it again creates gaps
 
         //Also, if one sound loop is shorter than the other soundloops, will wait untill
         //other sound loops finished to start playing again...
 
         if (!audioSource.isPlaying) {
             audioSource.PlayOneShot (audioClip1, audioClipVolume1);
             audioSource.PlayOneShot (audioClip2, audioClipVolume2);
             audioSource.PlayOneShot (audioClip3, audioClipVolume3);
         }
 
         //Tried changing clip
 
         //AudioSource can load only one clip, but allows for gapless looping
 
         audioSource.clip = audioClip2;
         audioSource.volume = audioClipVolume2;
         if (!audioSource.isPlaying) {
             audioSource.Play ();
         }
     }
 }
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
0
Best Answer

Answer by DemSec · Aug 10, 2016 at 05:13 AM

I took SuperStarPL's advice and created an Audio Source for each sound.

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
1

Answer by SuperStarPL · Jun 22, 2016 at 03:59 PM

If you want seamless/gap free transitions, you should use 3 Audio Sources playing at the same time, with only one's volume set to > 0. Then, you crossfade volumes using Lerp or something similar.

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 DemSec · Jun 22, 2016 at 10:18 PM 0
Share

Doesn't having multiple Audio Sources slows down the game more than scripting it out? Also, could I have all 3 audio sources on one object and differentiate between them? if so, could you give me a C# script example? Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Looping an audio while holding "shift key" 1 Answer

Play sound effect throughtout an entire animation clip 1 Answer

[Rhytm game] How to spawn on exact time of music? 3 Answers

How to get C# Array to Load into Audio Source 0 Answers

AudioClip stuttering while loop is OFF and not using function Update? 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