Click or drag to resize

Importing songs at runtime

AudioImporter package

To import songs at runtime a separate package needs to be imported into the project.

AudioImporter.unitypackage

The package contains 3 different importers:

  • MobileImporter, which is for mobile platforms only. It uses Unity's WWW / UnityWebRequest class, that can load mp3 files on mobile platforms only.

  • NAudioImporter, which uses NAudio, which is licensed under the Ms-PL. It might require licensing for using MP3 technology.

  • BassImporter, which uses Bass.dll and Bass.net, that require separate licenses for commercial projects. This importer can use the OS's mp3 decoder, so no MP3 license is needed.

MP3 technology is patented and might need additional licensing. More information

If you're not planning on using one of the importers, just delete its associated assets.

BassImporter needs two additional steps:

  1. Get bass.dll from http://www.un4seen.com/ and place the required libraries (based on the editor platform and the project's build target) in a Plugins folder.

  2. Get bass.net.dll from http://bass.radio42.com/ and place it somewhere your project's assets folder. Bass.Net comes with a number of versions. Unity needs the one from the "V2.0" folder. You can register Bass.Net on http://bass.radio42.com/bass_register.html and by editing line 17 of BassImporter.cs.

Important note Important

In order for some of the importers to work properly, the API compatibility level needs to be set to ".NET 2.0" in player settings. Otherwise it will only work in the editor, if at all.

For Android, Write Permission has to be set to External.

Importing a song

The AudioImporter Components have two methods for importing a song. Import() and ImportStreaming(). Import() imports the whole file at once, while ImportStreaming() imports a file gradually. This makes the song available for playback much faster, because it can be imported while it is being played.

After using Import() or ImportStreaming(), the importer will start loading the song into an AudioClip. This happens in the background as much as possible. Use the Loaded event to obtain the audioclip.

In Unity 5.3 or newer, these methods return a CustomYieldInstruction named ImportOperation, which can be used in a coroutine.

Browser

The package also includes a simple file browser, in the form of a prefab with an attached Browser Component. The browser uses Unity's UI system, so the prefab has to be parented to a Canvas.

Browser

Use the Browser Component's FileSelected event to handle selected files.

C#
using UnityEngine;

public class ImporterExample : MonoBehaviour 
{
    public Browser browser;
    public AudioImporter importer;
    public AudioSource audioSource;

    void Awake()
    {
        browser.FileSelected += OnFileSelected;
        importer.Loaded += OnLoaded;
    }

    private void OnFileSelected(string path)
    {
        importer.ImportStreaming(path);
    }

    private void OnLoaded(AudioClip clip)
    {
        audioSource.clip = clip;
        audioSource.Play();
    }
}