AudioSource.PlayOneShot volume increase and clipping
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have been trying to learn about Audio in Unity and am facing an issue where the volume increases to the point where it is noticeably clipping.
The sound is triggered by
GetComponent<AudioSource>().PlayOneShot(sound);
and it seems that it sometimes gets triggered multiple times, which leads to a volume increase and sometimes clipping.
I have tried using Play()
and Stop()
to solve the issue, but this led to all of the other audio being cut too. I haven't had much success trying to create new Audio Sources, but this might be due to my lack of knowledge in this area.
What would be the best way to prevent this issue? Can I declare multiple Audio Sources from one game object or should I be looking elsewhere?
Thank you!
unity3d audio
add a comment |
I have been trying to learn about Audio in Unity and am facing an issue where the volume increases to the point where it is noticeably clipping.
The sound is triggered by
GetComponent<AudioSource>().PlayOneShot(sound);
and it seems that it sometimes gets triggered multiple times, which leads to a volume increase and sometimes clipping.
I have tried using Play()
and Stop()
to solve the issue, but this led to all of the other audio being cut too. I haven't had much success trying to create new Audio Sources, but this might be due to my lack of knowledge in this area.
What would be the best way to prevent this issue? Can I declare multiple Audio Sources from one game object or should I be looking elsewhere?
Thank you!
unity3d audio
add a comment |
I have been trying to learn about Audio in Unity and am facing an issue where the volume increases to the point where it is noticeably clipping.
The sound is triggered by
GetComponent<AudioSource>().PlayOneShot(sound);
and it seems that it sometimes gets triggered multiple times, which leads to a volume increase and sometimes clipping.
I have tried using Play()
and Stop()
to solve the issue, but this led to all of the other audio being cut too. I haven't had much success trying to create new Audio Sources, but this might be due to my lack of knowledge in this area.
What would be the best way to prevent this issue? Can I declare multiple Audio Sources from one game object or should I be looking elsewhere?
Thank you!
unity3d audio
I have been trying to learn about Audio in Unity and am facing an issue where the volume increases to the point where it is noticeably clipping.
The sound is triggered by
GetComponent<AudioSource>().PlayOneShot(sound);
and it seems that it sometimes gets triggered multiple times, which leads to a volume increase and sometimes clipping.
I have tried using Play()
and Stop()
to solve the issue, but this led to all of the other audio being cut too. I haven't had much success trying to create new Audio Sources, but this might be due to my lack of knowledge in this area.
What would be the best way to prevent this issue? Can I declare multiple Audio Sources from one game object or should I be looking elsewhere?
Thank you!
unity3d audio
unity3d audio
asked Nov 27 '18 at 1:53
Chris IllusionChris Illusion
102315
102315
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Are you using an audio source for each gameobject you want to play a sound? If so, try to create a single gameobject (let's call Audio Source Holder) that only contains an audio source component.
With that in mind, you can have a reference to this Audio Source component and call PlayOneShot(sound) from the script you want to trigger the sound.
For example, let's suppose every time a bullet is fired, we want to play the sound:
public class Bullet : MonoBehaviour {
// This AudioSource reference comes from the Audio Source Holder gameobject
public AudioSource audioSource;
public void PlayFireSound(){
audioSource.PlayOneShot(sound);
}
}
Hope the above helps. You can also take a look at this question where the audio file is changed to an uncompressed format, like .wav, to see if your problem is solved.
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
add a comment |
Managed to fix it!
The issue was, that there was only a single audio source available. Therefore the solution is to create a second audio source and in the script use
public AudioSource sounds;
void PlaySound (AudioClip sound)
{
sounds = GetComponents<AudioSource>();
AudioSource audioSource = sounds[1]; //Using the second AudioSource
audioSource.clip = sound;
audioSource.Stop(); //Stop any previous audio from playing
audioSource.Play();
}
Big thank you to - https://answers.unity.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
EDIT: Improved code. The above code again would allow only one clip to play at a time, so I decided to take it further and create code for each clip as it starts playing. Here is the final code
public Dictionary<string,AudioSource> sounds = new Dictionary<string, AudioSource>();
void PlayOneShotSound (AudioClip sound)
{
//Check if AudioSource has been created for this clip
if (!sounds.ContainsKey(sound.name))
{
sounds.Add(sound.name, gameObject.AddComponent<AudioSource>());
}
AudioSource audioSource = sounds[sound.name]; //Using AudioSource for the clip
audioSource.clip = sound;
audioSource.Stop(); //Preventing previous sound from being played
audioSource.Play();
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491658%2faudiosource-playoneshot-volume-increase-and-clipping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you using an audio source for each gameobject you want to play a sound? If so, try to create a single gameobject (let's call Audio Source Holder) that only contains an audio source component.
With that in mind, you can have a reference to this Audio Source component and call PlayOneShot(sound) from the script you want to trigger the sound.
For example, let's suppose every time a bullet is fired, we want to play the sound:
public class Bullet : MonoBehaviour {
// This AudioSource reference comes from the Audio Source Holder gameobject
public AudioSource audioSource;
public void PlayFireSound(){
audioSource.PlayOneShot(sound);
}
}
Hope the above helps. You can also take a look at this question where the audio file is changed to an uncompressed format, like .wav, to see if your problem is solved.
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
add a comment |
Are you using an audio source for each gameobject you want to play a sound? If so, try to create a single gameobject (let's call Audio Source Holder) that only contains an audio source component.
With that in mind, you can have a reference to this Audio Source component and call PlayOneShot(sound) from the script you want to trigger the sound.
For example, let's suppose every time a bullet is fired, we want to play the sound:
public class Bullet : MonoBehaviour {
// This AudioSource reference comes from the Audio Source Holder gameobject
public AudioSource audioSource;
public void PlayFireSound(){
audioSource.PlayOneShot(sound);
}
}
Hope the above helps. You can also take a look at this question where the audio file is changed to an uncompressed format, like .wav, to see if your problem is solved.
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
add a comment |
Are you using an audio source for each gameobject you want to play a sound? If so, try to create a single gameobject (let's call Audio Source Holder) that only contains an audio source component.
With that in mind, you can have a reference to this Audio Source component and call PlayOneShot(sound) from the script you want to trigger the sound.
For example, let's suppose every time a bullet is fired, we want to play the sound:
public class Bullet : MonoBehaviour {
// This AudioSource reference comes from the Audio Source Holder gameobject
public AudioSource audioSource;
public void PlayFireSound(){
audioSource.PlayOneShot(sound);
}
}
Hope the above helps. You can also take a look at this question where the audio file is changed to an uncompressed format, like .wav, to see if your problem is solved.
Are you using an audio source for each gameobject you want to play a sound? If so, try to create a single gameobject (let's call Audio Source Holder) that only contains an audio source component.
With that in mind, you can have a reference to this Audio Source component and call PlayOneShot(sound) from the script you want to trigger the sound.
For example, let's suppose every time a bullet is fired, we want to play the sound:
public class Bullet : MonoBehaviour {
// This AudioSource reference comes from the Audio Source Holder gameobject
public AudioSource audioSource;
public void PlayFireSound(){
audioSource.PlayOneShot(sound);
}
}
Hope the above helps. You can also take a look at this question where the audio file is changed to an uncompressed format, like .wav, to see if your problem is solved.
answered Nov 27 '18 at 2:38
stamblewstamblew
618
618
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
add a comment |
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
Thank you for your reply! The main issue I am having is the volume increase which comes from the audio being played multiple times at once. Wouldn't the above code have the same problem?
– Chris Illusion
Nov 27 '18 at 2:55
add a comment |
Managed to fix it!
The issue was, that there was only a single audio source available. Therefore the solution is to create a second audio source and in the script use
public AudioSource sounds;
void PlaySound (AudioClip sound)
{
sounds = GetComponents<AudioSource>();
AudioSource audioSource = sounds[1]; //Using the second AudioSource
audioSource.clip = sound;
audioSource.Stop(); //Stop any previous audio from playing
audioSource.Play();
}
Big thank you to - https://answers.unity.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
EDIT: Improved code. The above code again would allow only one clip to play at a time, so I decided to take it further and create code for each clip as it starts playing. Here is the final code
public Dictionary<string,AudioSource> sounds = new Dictionary<string, AudioSource>();
void PlayOneShotSound (AudioClip sound)
{
//Check if AudioSource has been created for this clip
if (!sounds.ContainsKey(sound.name))
{
sounds.Add(sound.name, gameObject.AddComponent<AudioSource>());
}
AudioSource audioSource = sounds[sound.name]; //Using AudioSource for the clip
audioSource.clip = sound;
audioSource.Stop(); //Preventing previous sound from being played
audioSource.Play();
}
add a comment |
Managed to fix it!
The issue was, that there was only a single audio source available. Therefore the solution is to create a second audio source and in the script use
public AudioSource sounds;
void PlaySound (AudioClip sound)
{
sounds = GetComponents<AudioSource>();
AudioSource audioSource = sounds[1]; //Using the second AudioSource
audioSource.clip = sound;
audioSource.Stop(); //Stop any previous audio from playing
audioSource.Play();
}
Big thank you to - https://answers.unity.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
EDIT: Improved code. The above code again would allow only one clip to play at a time, so I decided to take it further and create code for each clip as it starts playing. Here is the final code
public Dictionary<string,AudioSource> sounds = new Dictionary<string, AudioSource>();
void PlayOneShotSound (AudioClip sound)
{
//Check if AudioSource has been created for this clip
if (!sounds.ContainsKey(sound.name))
{
sounds.Add(sound.name, gameObject.AddComponent<AudioSource>());
}
AudioSource audioSource = sounds[sound.name]; //Using AudioSource for the clip
audioSource.clip = sound;
audioSource.Stop(); //Preventing previous sound from being played
audioSource.Play();
}
add a comment |
Managed to fix it!
The issue was, that there was only a single audio source available. Therefore the solution is to create a second audio source and in the script use
public AudioSource sounds;
void PlaySound (AudioClip sound)
{
sounds = GetComponents<AudioSource>();
AudioSource audioSource = sounds[1]; //Using the second AudioSource
audioSource.clip = sound;
audioSource.Stop(); //Stop any previous audio from playing
audioSource.Play();
}
Big thank you to - https://answers.unity.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
EDIT: Improved code. The above code again would allow only one clip to play at a time, so I decided to take it further and create code for each clip as it starts playing. Here is the final code
public Dictionary<string,AudioSource> sounds = new Dictionary<string, AudioSource>();
void PlayOneShotSound (AudioClip sound)
{
//Check if AudioSource has been created for this clip
if (!sounds.ContainsKey(sound.name))
{
sounds.Add(sound.name, gameObject.AddComponent<AudioSource>());
}
AudioSource audioSource = sounds[sound.name]; //Using AudioSource for the clip
audioSource.clip = sound;
audioSource.Stop(); //Preventing previous sound from being played
audioSource.Play();
}
Managed to fix it!
The issue was, that there was only a single audio source available. Therefore the solution is to create a second audio source and in the script use
public AudioSource sounds;
void PlaySound (AudioClip sound)
{
sounds = GetComponents<AudioSource>();
AudioSource audioSource = sounds[1]; //Using the second AudioSource
audioSource.clip = sound;
audioSource.Stop(); //Stop any previous audio from playing
audioSource.Play();
}
Big thank you to - https://answers.unity.com/questions/52017/2-audio-sources-on-a-game-object-how-use-script-to.html
EDIT: Improved code. The above code again would allow only one clip to play at a time, so I decided to take it further and create code for each clip as it starts playing. Here is the final code
public Dictionary<string,AudioSource> sounds = new Dictionary<string, AudioSource>();
void PlayOneShotSound (AudioClip sound)
{
//Check if AudioSource has been created for this clip
if (!sounds.ContainsKey(sound.name))
{
sounds.Add(sound.name, gameObject.AddComponent<AudioSource>());
}
AudioSource audioSource = sounds[sound.name]; //Using AudioSource for the clip
audioSource.clip = sound;
audioSource.Stop(); //Preventing previous sound from being played
audioSource.Play();
}
edited Nov 27 '18 at 18:33
answered Nov 27 '18 at 6:37
Chris IllusionChris Illusion
102315
102315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491658%2faudiosource-playoneshot-volume-increase-and-clipping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown