mercredi 6 mars 2019

How to create AudioClip and play it without a use of AudioSource?

I am trying to create a simple beep sound in Unity and play it without a use of AudioSource.
I have this method that plays an AudioClip with the help of Reflection.

public static void PlayClip(AudioClip clip)
{
    Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
    Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
    MethodInfo method = audioUtilClass.GetMethod(
        "PlayClip",
        BindingFlags.Static | BindingFlags.Public,
        null,
        new System.Type[] {
            typeof(AudioClip)
    },
    null
    );
    method.Invoke(
        null,
        new object[] {
            clip
    }
    );
}

Whenever I call a method that is suppose to play the sound I create a brand new AudioClip and I call this PlayClip() method and I pass the new created clip.

public void PlayBeep()
{
    AudioClip clip = AudioClip.Create("Beep", 48000 * 2, 1, 48000, false);
    AudioUtility.PlayClip(clip);
}

But the problem is that I cannot hear anything. What am I doing wrong? And are there any other ways that could help me in achieving this type of result?





Aucun commentaire:

Enregistrer un commentaire