Real time stream playing in WebGL by AudioSorce with AudioClip – Unity Engine

I’m experiencing an issue with real-time audio streaming in WebGL when using a looping AudioSource with a dynamically updated AudioClip. The audio playback is choppy.

_audioSource.loop = true;
_audioSource.clip = AudioClip.Create("VoiceChat", _bufferCapacity, 1, _frequency, false); // Streaming set to false

_audioSource.clip.SetData(_ringBuffer, 0); // update all clip with new data

Decompression is fast—significantly faster than the duration of a single audio packet. However, there is always not enough buffered audio in the clip for continuous playback, causing the AudioSource to stop until new audio data is written. This results in noticeable gaps and interruptions.

What are the best approaches for playing real-time voice audio in Unity WebGL?
Can this be achieved using Unity’s Audio API, or is it better to handle audio playback in JavaScript via the Web Audio API using a Unity WebGL plugin?

VoiceChatPlayer initialized. Audio clip size: 6400, Frequency: 8000

Unity output sample rate: 44100 Hz

AudioClip sample rate: 8000 Hz length: 0.8 seconds channels: 1

…

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 148480, totalAmountOfPlayedSamplesFromClip is 147916

AudioBufferManager::EnqueuePacket::Current count of packets is 0

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 0, totalAmountOfPlayedSamplesFromClip is 0

AudioBufferManager::Update::Packet will be decompressed. Current count of packets is 1

Taken time to decompress since recieving: 0.01200008

Copying 640 samples to audio buffer. Index before: 0

Added sample time to received. Received: 640.00, SampleTime: 0.08

Taken time to decompress + write to buffer since recieving: 0.01300001

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 640, totalAmountOfPlayedSamplesFromClip is 0

VoiceChatPlayer::Update::Start playback with delay 0.5

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 640, totalAmountOfPlayedSamplesFromClip is 661

VoiceChatPlayer::Update::Played too far. Audio source will be stopped. Played: 661, Written: 640

Stopping audio playback. Resetting internal state.

Read more here: Source link

Real time stream playing in WebGL by AudioSorce with AudioClip – Unity Engine

I’m experiencing an issue with real-time audio streaming in WebGL when using a looping AudioSource with a dynamically updated AudioClip. The audio playback is choppy.

_audioSource.loop = true;
_audioSource.clip = AudioClip.Create("VoiceChat", _bufferCapacity, 1, _frequency, false); // Streaming set to false

_audioSource.clip.SetData(_ringBuffer, 0); // update all clip with new data

Decompression is fast—significantly faster than the duration of a single audio packet. However, there is always not enough buffered audio in the clip for continuous playback, causing the AudioSource to stop until new audio data is written. This results in noticeable gaps and interruptions.

What are the best approaches for playing real-time voice audio in Unity WebGL?
Can this be achieved using Unity’s Audio API, or is it better to handle audio playback in JavaScript via the Web Audio API using a Unity WebGL plugin?

VoiceChatPlayer initialized. Audio clip size: 6400, Frequency: 8000

Unity output sample rate: 44100 Hz

AudioClip sample rate: 8000 Hz length: 0.8 seconds channels: 1

…

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 148480, totalAmountOfPlayedSamplesFromClip is 147916

AudioBufferManager::EnqueuePacket::Current count of packets is 0

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 0, totalAmountOfPlayedSamplesFromClip is 0

AudioBufferManager::Update::Packet will be decompressed. Current count of packets is 1

Taken time to decompress since recieving: 0.01200008

Copying 640 samples to audio buffer. Index before: 0

Added sample time to received. Received: 640.00, SampleTime: 0.08

Taken time to decompress + write to buffer since recieving: 0.01300001

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 640, totalAmountOfPlayedSamplesFromClip is 0

VoiceChatPlayer::Update::Start playback with delay 0.5

VoiceChatPlayer::Update::_totalAmountOfWrittenSamplesToClip is 640, totalAmountOfPlayedSamplesFromClip is 661

VoiceChatPlayer::Update::Played too far. Audio source will be stopped. Played: 661, Written: 640

Stopping audio playback. Resetting internal state.

Read more here: Source link