- Home /
Code for playing sound in editor in 5.5.0 does not appear to work in 2017.2
Hi! So I have this editor tool I'm working on and a feature of the tool is that you can select a sound, see the sound waves drawn, and play the sound. The same code works in 5.5.0p1, but does not seem to work in 2017.2. I hear sound in 5.5.0p1, but not in Unity 2017.2. I have used Ilspy to look at the UnityEditor dll and the function calls I'm making should work, but strangely no sound is played in Unity 2017.2.
void PlayAudioClip(AudioClip Clip)
{
System.Type AudioUtilType = typeof(AudioImporter).Assembly.GetType("UnityEditor.AudioUtil");
Debug.Log(AudioUtilType);
Debug.Log(Clip);
MethodInfo Method = AudioUtilType.GetMethod("PlayClip", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip)/*, typeof(int), typeof(bool)*/ }, null);
MethodInfo MethodIsPlaying = AudioUtilType.GetMethod("IsClipPlaying", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip)/*, typeof(int), typeof(bool)*/ }, null);
Method.Invoke(null, new object[] { Clip/*, 0, false*/ });
Debug.Log(MethodIsPlaying.Invoke(null, new object[] { Clip/*, 0, false*/ }));
}
I have put several debugs in here to try and make sure the type is still valid and the clip is not null. I also went and called the IsClipPlaying function and this function always returns true right after my call to play the audio clip but still, nothing is heard. Please help, as I'm not at all sure what could be wrong. I was calling this function :
public static extern void PlayClip(AudioClip clip, [DefaultValue("0")] int startSample, [DefaultValue("false")] bool loop);
but then tried to call :
[ExcludeFromDocs]
public static void PlayClip(AudioClip clip)
{
bool loop = false;
int startSample = 0;
AudioUtil.PlayClip(clip, startSample, loop);
}
to see if it would make a difference somehow, but it does not. I didn't really expect it to since it eventually calls the same function but I am so confused by this that I'm willing to try anything just to get it to work.
I know this may sound super dumb but people do make mistakes like this without noticing so I am going to ask this. Please do not be offended. Have you checked in the top right of your screen where there is a button called '$$anonymous$$ute Audio'.
I may have got the wrong idea because I think you are making a script that runs before runtime. $$anonymous$$aybe you accidentally have Unity Audio muted? Hopefully this helps
I've already checked that. The $$anonymous$$ute Audio button is not clicked. Also I'm pretty sure that only affects things in game and not in the editor. I also checked my sound mixer on my computer and the sound is not muted for Unity. It works just fine in 5.5.0p1. I also noticed that the audio inspector in Unity 2017.2 and 2017.3 does not play anything either. I even downloaded Unity 2018.1.0b2 and it still has the same issue. Sound does play in game though.
Try first a simpler audio playing before you implement your code into higher version :)
Your answer
Follow this Question
Related Questions
Some technical questions regarding Unity audio compression 0 Answers
Mark gameobject field as changed from prefab 2 Answers
Custom Inspector: Accessing a reference to another MonoBehaviour? 1 Answer
Unity inspector scripting - scriptable objects - card game 0 Answers
How to properly handle Undo events in custom inspector? 0 Answers