Using FMOD for C#?

别说谁变了你拦得住时间么 提交于 2019-12-01 16:45:39

Fmod is written in unmanaged C++ so you cannot reference it directly from a .Net application. There is a c# wrapper to the fmodex.dll in the fmod package under a directory called "fmod_wrapper" if I am not mistaken that you can add to your project and which will take care of making the P/Invoking for you.

Try https://github.com/madrang/FmodSharp been working for a little while on it. It should be better than the current Fmod wrapper.

Instead of using Handles and coding like you are using C++ in C#. The FmodSharp wrapper is object oriented and there is no need to think about using handles.

public static void Main (string[] args)
{
    Console.WriteLine ("Fmod Play File Test");

    Xpod.FmodSharp.Debug.Level = Xpod.FmodSharp.DebugLevel.Error;
    var SoundSystem = new Xpod.FmodSharp.SoundSystem.SoundSystem();

    Console.WriteLine ("Default Output: {0}", SoundSystem.Output);

    SoundSystem.Init();
    SoundSystem.ReverbProperties = Xpod.FmodSharp.Reverb.Presets.Room;

    if (args.Length > 0) {
        foreach (string StringItem in args) {
            Xpod.FmodSharp.Sound.Sound SoundFile;
            SoundFile = SoundSystem.CreateSound (StringItem);

            Xpod.FmodSharp.Channel.Channel Chan;
            Chan = SoundSystem.PlaySound(SoundFile);

            while(Chan.IsPlaying) {
                System.Threading.Thread.Sleep(10);
            }

            SoundFile.Dispose();
            Chan.Dispose();
        }

    } else {
        Console.WriteLine ("No File to play.");
    }

    SoundSystem.CloseSystem();
    SoundSystem.Dispose();
}

In order to use FMOD in C# you need to wrap FMOD functions and structures using p/Invoke. This task has been taken care of for you in the open source project fmodnet

To add a COM item to a VS project perform the following steps:

  • Register the COM

  • Select "Add Reference"

  • Select COM tab

  • Browse to item and select it.

You should see (after a build) that VS has created an Interopt DLL library in your BIN dir.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!