Skip to content

Add Support for Unity AudioSources

This MR adds the ability to output VLC Audio to a Unity AudioSource. This will add the ability to apply Unity effects to audio, have 3D audio, control audio from Unity (i.e. audioSource.Volume), and even direct Media audio to spatializers (i.e. SteamAudio).

While this MR does have basic functionality, I have discovered some slight issues with certain media types. For example, streams seem to cause the most issues with de-sync (video being slightly ahead of audio). More extensive testing is recommended before merging to verify functionality.

The VLCAudioSource component is designed to be used as an API, integrating with any examples (where this MR even implements support in the VLCPlayerExample component used in the 3D example scene). Below is a step by step process on how to integrate the VLCAudioSource component (assuming you already have a MediaPlayer).

  1. Create a GameObject with an AudioSource
    • The VLCAudioSource component MUST be attached to a GameObject with an AudioSource
  2. In the script that handles the Media Player, add a method to initialize the component
// Set this in your Inspector or before you set the value of the vlcAudioSource field
public AudioSource audioSource;
private VLCAudioSource vlcAudioSource;

void Start()
{
    // Add a VLCAudioSource component
    vlcAudioSource = audioSource.gameObject.AddComponent<VLCAudioSource>();
}
  1. Whenever you go to create your MediaPlayer, pass that into the NewMedia(MediaPlayer) function
void CreateMediaPlayer()
{
    mediaPlayer = new MediaPlayer(libVLC);
    vlcAudioSource.NewMedia(mediaPlayer);
}

and that's it! Your VLC Player will now output audio to the specified AudioSource. If you want to, you can also specify the rate and channels for the audio to use.

void Start()
{
    // Add a VLCAudioSource component
    vlcAudioSource = audioSource.gameObject.AddComponent<VLCAudioSource>();
    // Update the rate
    vlcAudioSource.SampleRate = 41000;
    // Update the channels
    vlcAudioSource.Channels = 1;
}

For more information, just take a look at the VLCAudioSource script (as I left plenty of comments on what all it does) or feel free to ask me.

Happy reviewing! 😸

Related Issues

Merge request reports

Loading