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 S_Byrnes · May 27, 2014 at 12:12 PM · audioaudio priorityforce stop

How To Stop An Audio Clip if it's Playing More Than 2 Sounds?

Hey guys, I've been trying to figure out how to stop playing the first audio file if two more start playing while the first is still going.

Because right now, if more than two are playing, I get a very bad white noise effect.

Does anyone know how I can achieve this?

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 Klarax · May 27, 2014 at 01:19 PM 0
Share

use a timer, roughly 2000.0f is 2 seconds. you an find out how to do it by exact time, but i forgot.

when you play - set a bool to true start timer - i.e. timer--;

when timer

1 Reply

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

Answer by PouletFrit · May 27, 2014 at 02:48 PM

you will probly need to have something that will count the number of sound playing and that keep tracks of the sounds playing. Each time an audioSource start (or audioClip or whatever ur using), add it to a list and check if that list got bigger than 2, if yes, stop the first sound and remove it from the list.

Also you can either set a timer the length of the sound to remove itself from the list after it have done playing (if ur not looping ur sound) or go thourgh all the sounds of the list to check if they are still playing when ur adding a new one

     using UnityEngine;
     using System.Collections.Generic;
     
     public class AudioManager : MonoBehaviour {
         // The number of maximum sounds to allow playing simultaneously
         public int maxSoundPlaying = 2;
     
         private List<AudioSource> soundsPlaying = new List<AudioSource>();
     
         public void PlaySound(AudioSource sound) {
             // First remove all the sounds that are done playing
             int i = 0;        
             while (i < soundsPlaying.Count) {
                 if (soundsPlaying[i].isPlaying) {
                     i++;
                 } else {
                     soundsPlaying.RemoveAt(i);
                 }
             }
 
             // Then start the sound and add it to the list
             sound.Play();
             soundsPlaying.Add(sound);
 
             // Check if there is too much sound playing, 
             // if yes remove the first one of the list
             if (soundsPlaying.Count > maxSoundPlaying) {
                 soundsPlaying[0].Stop();
                 soundsPlaying.RemoveAt(0)
             }
         }
     }
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 S_Byrnes · May 27, 2014 at 11:03 PM 0
Share

Thanks for the excellent answer! I just tried it but I can still here 3 sounds at once. I just attached this to my main camera that has an audio listener on it. Is that where it's supposed to go? Because all my sounds come directly from objects and are not all in one list.

avatar image S_Byrnes · May 27, 2014 at 11:25 PM 0
Share

I have this attached to my objects that have audio on them:

 void OnCollisionEnter(Collision collision) {
         audio.Play ();
     }

I've tried combining your script with this but I think it's not picking up on it because it's sound.Play and not audio.Play

avatar image PouletFrit · May 28, 2014 at 11:45 AM 1
Share

You need to call the function named PlaySound of my script and pass the audioSource containing the sound you wish to play as an argument.

So attach my script to your camera like u did and on your script that you have:

 void OnCollisionEnter(Collision collision) {
     audio.Play ();
 }

replace it by:

 public Audio$$anonymous$$anager audio$$anonymous$$anager;
 
 void OnCollisionEnter(Collision collision) {
     audio$$anonymous$$anager.PlaySound(this.audio);
 }

you should now see a variable named audio manager in ur script, drag and drop the camera with my script attached to it.

It should now work

avatar image S_Byrnes · May 28, 2014 at 12:54 PM 0
Share

Hmmm, I just tried to drag the camera into that field but it won't let me, I even tried to just drag that script into it and it wouldn't let me do that either

avatar image PouletFrit · May 29, 2014 at 11:50 AM 0
Share

You attached the Audio$$anonymous$$anager script to the camera and it didnt let u add that script to that field?

Add Component --> Audio $$anonymous$$anager, while the camera game object is selected

Click on the circle next to Audio $$anonymous$$anager on your script that start the audio and select Camera

if it still doesn't work try adding this on ur script that start the audio:

 public Audio$$anonymous$$anager audio$$anonymous$$anager;
 
 void Start() {
     audio$$anonymous$$anager = Camera.main.GetComponent<Audio$$anonymous$$anager>();
 }

as long as u added the audio$$anonymous$$anager to the gameObject containing the main camera it should work

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

22 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

Related Questions

Audio Sounds Blurring Together 0 Answers

Play Audio from different Object 1 Answer

Play audio clip on more than one object from another gameobject 2 Answers

How can i choose what channel to play my audio by myself? 1 Answer

Why does sound on Android play at double speed and crackle? 3 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