Jump to content

How can I play audio from a file without using Effects?


Recommended Posts

So I've been adding some stuff to DangIt! to make it so there is an alarm when something fails, and have been having some problems. Basically, I want to play a sound that I get from GameDatabase, without using an EFFECTS block, because I don't want to blanket-add an EFFECTS to all of the parts in the game.

Current code:


print("starting alarm");
if (this.AlarmAudio==null)
{
print ("create source");
this.AlarmAudio=new AudioSource();
}

if (this.audio.clip==null)
{
print ("create clip");
this.AlarmAudio.clip=GameDatabase.Instance.GetAudioClip("DangIt/Sounds/alarm"); //Load alarm sound
}

if (DangIt.Instance.CurrentSettings.SoundNotifications)
{
print ("play alarm");
int i=0;
{
print("loop:"+i.ToString());
print("delay:"+(this.audio.clip.length*i).ToString());
this.AlarmAudio.PlayDelayed(this.audio.clip.length*i);
i++;
}
}

Full source on GitHub

And here is the error:


[LOG 22:14:32.187] starting alarm
[LOG 22:14:32.187] create source
[LOG 22:14:32.188] create clip
[LOG 22:14:32.190] [DangIt]: DangItReactionWheel[-167804][Ship: Untitled Space Craft]: ERROR:
at (wrapper managed-to-native) UnityEngine.AudioSource:set_clip (UnityEngine.AudioClip)
at ippo.FailureModule.Fail () [0x00000] in <filename unknown>:0

Full log on Pastebin

How do I do this? Its SO FRUSTRATING!

Link to comment
Share on other sites

You basically have it except that AudioSource is a Component that needs to be added to a GameObject, not directly instantiated. Try replacing this

this.AlarmAudio = new AudioSource();

with

this.AlarmAudio = gameObject.AddComponent<AudioSource>()

And then making sure your GameDatabase.Instance.GetAudioClip is not returning null.

Here's an example I made a while ago that plays a sound with a KSPAddon MonoBehaviour; you can do exactly the same with a PartModule

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...