WebGL Spatial audio for Videoplayer

Hi all. I have a webgl project and I’m having a problem. I have a videoplayer which pulls in an external video clip (via URL) - this is working fine, but I set the Audio Output Mode to Audio Source and then set up the audio source so that Spatial Blend is 1 (3D) and custom rolloff with correct max and min distance settings.

In the editor, it all works like a dream. When I build and run as webgl, the audio for the video player plays loudly and at the same level (volume) throughout the entire scene.

There are several other audio sources in the scene which are not associated with a video player and they all work as expected.

I read in another forum from a year or two ago that setting a videoplayer audio output to audio source is not supported in webgl (but that was a known bug a couple of years ago and I presume would have been fixed by now). Does anyone know of a fix for this?

Hi!

This is not a bug as much as a limitation of the HTML5

There exists other code paths in HTML5 media support that may, in some browsers, allow this, but this isn’t planned for the near future.

In the mean time, your best option would be to work with a separate audio clip and play this in sync with the (audio-less) video.

Hope this helps,

Dominique Leroux
A/V developer at Unity

1 Like

Thanks Dominique. Do you have any thoughts on how to synch the audio and video? I can use C# to load the audio from a URL (as the video is also loading from a URL) - is there a way to delay playback on both the videoplayer and the audiosource until both are loaded?

Sortof Solved but also sortof not lol

So as far as I can see, there is no way of streaming an external mp3 using webgl as a platform. In our project, loading time and project size are already problematic so we’re trying to externalize as much size-intensive assets (eg media) as possible. there must be a way to stream mpeg audio in webgl but setting that aside, if I just swallow the bad news and take the size hit, I can easily sync the video and the audio. See error message below lol

Streaming of ‘mpeg’ on this platform is not supported

ps - if there is a way to stream mpeg audio I’d love to hear about it - looked all over the webs and haven’t found anything yet.

pps - if there is not a way to stream mpeg audio, can this be added to the punchlist for future revisions? I’m sure I’m not alone in wanting to keep as much unnecessary weight out of the project size as possible and assets like audio clips and video tracks are huge… Thank you in advance!

Hi again!

The limitation of audio mp3 streaming seems to be one imposed by Unity, not by the browsers, possibly coming from an era where this wasn’t broadly supported (just guessing here…). We’ll test it out and remove this restriction if everything works out. It’s been pointed out earlier: Unity Issue Tracker - Platform not supported error is thrown when trying to play an MP3 file in editor with build platform set to WebGL so it’s already in the todo.

Looking at the current code, .wav may work but streaming of uncompressed audio is not a very exciting solution.

So for the time being having a built-in audio asset (not streamed) and a video stream seems to be the other compromise that’s available to you.

Sorry for all the trouble… Hope you find a way to your goal; let us know how this goes and we’ll see if there’s anything we can think of that can help.

Dominique

I am running into the same issue. I decided I will use built-in audio and stream the video (without sound)
I am clueless as to how I can sync the video and the audio. Does anyone have some clues or script I can look at?

I solved that problem for me. But only because I am interested into the audio fallof by distance.

I just controll the volume manually by the player distance to my videoplayer.
But I think with a dot-product and mutliple audio tracks you can also emulate the spread.

I have to say that I’m not an audio guy and just made it work on a ver simple level.

using UnityEngine;
using UnityEngine.Video;

public class VideoWebGL : MonoBehaviour
{
    [Header("References...")]
    public VideoPlayer Video;
    public Transform PlayersView;
    public string VideoLocation;

    [Header("Audio...")]
    public AnimationCurve distanceInterpolation = AnimationCurve.Linear(0, 0, 1, 1);
    public float minDistance = 1;
    public float maxDistance = 5;


    private void Awake()
    {
        Video.url = System.IO.Path.Combine(Application.streamingAssetsPath, VideoLocation);
    }

    private void Update()
    {
        var currentDistance = Vector3.Distance(transform.position, PlayersView.position);
        var relativeDistance = Mathf.Clamp01((currentDistance - minDistance) / (maxDistance - minDistance));
        var currentVolume = distanceInterpolation.Evaluate(1 - relativeDistance);
        Video.SetDirectAudioVolume(0, currentVolume);
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(.5f, .5f, 1, 1);
        Gizmos.DrawWireSphere(transform.position, minDistance);
        Gizmos.DrawWireSphere(transform.position, maxDistance);
    }
}
3 Likes

I love the way you think! Hahaha

You saved my day.

Just to save some time to the next users (I’m sure it is an obviousness for many people): You have to put the Video Player “Audio Output Mode” in “Direct” to take effect. :wink:

1 Like

I have spent 2 days debugging the same issue with 10 different unity versions, 5 browsers, and 3 OSes, dammit.
I don’t understand why this hasn’t been fixed yet, sad to lose all this time.

1 Like