Audio format importing

Hi,

Would it be ok to ask a question here (apologies if in incorrect forum section), regarding Audio format importing.
Older Unity versions only allow for .wav file audio import (at runtime) to my understanding, not .mp3 I believe due to licensing issues…
Will the 2020 version allow this, or can this be handled differently?

You can find a list of the supported formats in the manual. Mp3 has at least been supported since 5.2.

Hello LeonhardP, many thanks for your help. I will proceed to look for a sample script/how to, to be able to load external mp3’s into my game scene. The intent was to use a asset/plugin called audioimporter but if there is a native/easier way I would prefer that.

You can just drop the file into the project pane as with any other asset. Or are you talking about loading them at runtime?

Hi,I need to load at runtime.

You can use CSCore or NAudio to retrieve the samples from the audio file, which you feed into the PCMReaderCallback delegate on a runtime-created AudioClip. Here’s some example code taken from a project I’m working on, it uses the ISampleSource API from CSCore:

void PcmSetPositionCallback(int position)
{
    if (isDisposed || !SampleSource.CanSeek)
        return;

    SampleSource.Position = (long)position * SampleSource.WaveFormat.Channels;
}

void PcmReaderCallback(float[] samples)
{
    if (isDisposed)
        return;

    for (int offset = 0, remaining = samples.Length; remaining > 0;)
    {
        int n = SampleSource.Read(samples, offset, remaining);

        if (n <= 0)
            break;

        offset    += n;
        remaining -= n;
    }
}

Hope that’s useful.