Issue with playing an audio clip as a FMOD sound

Hello,

I have been looking for a way to play an unity audio clip as a FMOD sound.

There are solution for this query from the previous versions of FMOD : -
https://qa.fmod.com/t/load-an-audioclip-as-fmod-sound/11741
https://qa.fmod.com/t/err-invalid-param-when-converting-audioclip-to-fmod-sound/11776

But none of these solution are working for me I updated the the code according to the
current APIs please check my script attached in which you will find both the methods
I referred from the links above.
What should I do?

PLEASE HELP!!!
My Script.txt (3.3 KB)

Hi,

There are a few things that will need to be fixed before you can use Unity Audio Clips.

Firstly enable Unity Audio again under the Unity toolbar Edit drop-down → Project Settings → Audio → Disable Unity Audio. Keep in mind with Unity Audio re-enabled the FMOD audio will no longer be heard on PS5 and Xbox.

Currently channel_group isn’t part of the FMOD signal chain so when we are calling RuntimeManager.CoreSystem.playSound() with the channel_group as it is, it won’t be heard. The easier way to fix this is to assign channel_group to the MasterChannelGroup as this is where all sound eventually goes. To do this we will call FMODUnity.RuntimeManager.CoreSystem.getMasterChannelGroup(out channel_Group). So now when we call playSound() the audio from the clip will be heard as it is part of the MasterChannelGroup.

A tip, call Debug.Log() on the output of result each time it is assigned, rather than just at the end of the function. Logging the results value at the end of the function will just be its most recent assignment.

Lastly, make sure that channel.setPaused() is set to false to allow the sound to play.

Hopefully, this will allow the Audio Clip to play!

Thanks @Connor_FMOD
It’s working now!!!

1 Like

For ease to anyone else who may find this in the future, here’s the merged code that worked for me:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using FMOD;
using FMODUnity;

public class ClipToSound : Singleton<ClipToSound>
{
    public AudioClip audioclip;
    Channel channel;
    private ChannelGroup channelGroup;

    public void Start()
    {
        float[] samples = new float[audioclip.samples * audioclip.channels];
        audioclip.GetData(samples, 0);

        uint lenbytes = (uint)(audioclip.samples * audioclip.channels * sizeof(float));

        CREATESOUNDEXINFO soundinfo = new CREATESOUNDEXINFO();
        soundinfo.cbsize = Marshal.SizeOf(typeof(CREATESOUNDEXINFO));
        soundinfo.length = lenbytes;
        soundinfo.format = SOUND_FORMAT.PCMFLOAT;
        soundinfo.defaultfrequency = audioclip.frequency;
        soundinfo.numchannels = audioclip.channels;

        RESULT result;
        Sound sound;
        result = RuntimeManager.CoreSystem.createSound("abc", MODE.OPENUSER, ref soundinfo, out sound);

        IntPtr ptr1, ptr2;
        uint len1, len2;
        result = sound.@lock(0, lenbytes, out ptr1, out ptr2, out len1, out len2);
        Marshal.Copy(samples, 0, ptr1, (int)(len1 / sizeof(float)));
        if (len2 > 0)
        {
            Marshal.Copy(samples, (int)(len1 / sizeof(float)), ptr2, (int)(len2 / sizeof(float)));
        }
        result = sound.unlock(ptr1, ptr2, len1, len2);
        result = sound.setMode(MODE.LOOP_NORMAL);

        FMODUnity.RuntimeManager.CoreSystem.getMasterChannelGroup(out channelGroup);
        result = RuntimeManager.CoreSystem.playSound(sound, channelGroup, false, out channel);
        channel.setPaused(false);
        UnityEngine.Debug.LogError("RESULT => " + result);
    }
}

Singleton is a custom class I have (presumably similar to OP) and is optional.

I know it’s an old topic, but I’m completely stuck on this. I tried using d23’s code above, it runs fine, and I can see a sound channel playing but there’s no volume (neither from the speakers or in the debug panel).

The sound file works fine if I use FMODUnity.RuntimeManager.CoreSystem.createSound(path, FMOD.MODE.DEFAULT, out fmodSound).

But this doesn’t work on android, so it would be better to create it from an audioclip.

The sound file is .wav file with the following settings;

  • Decompress on load
  • Preload Audio data
  • PCM

Any clues what could be wrong?