Skip to content

StreamAudioSource.SampleCallbackAsync() crashes on Unity 6 and 6.2 #438

@dudziakl

Description

@dudziakl

Bug Report

The issue applies to the Android build. PC is working fine. I didn't try iOS.

Overview

After the change of the SampleCallback in StreamingAudioSource from BufferCallback() to SampleCallackAsync(), there is a serious crash that closes the app on Android. It happens on Unity 6/6.2. Older Unity versions like 2021.3.45f2, 2022.3.62f2 are working fine.

private async Task GenerateSpeechAsync(string text, CancellationToken cancellationToken)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(text)) { return; }
                var request = new SpeechRequest(input: text, model: Model.TTS_1, voice: voice, responseFormat: SpeechResponseFormat.PCM);
                var stopwatch = Stopwatch.StartNew();
                using var speechClip = await openAI.AudioEndpoint.GetSpeechAsync(
                    request,
                    partialClip => streamAudioSource.SampleCallbackAsync(partialClip.AudioSamples), //<-- it crashes in this line
                    cancellationToken)
                    .ConfigureAwait(true);
                var playbackTime = speechClip.Length - (float)stopwatch.Elapsed.TotalSeconds + 0.1f;

To Reproduce

I have used Sample project with version 8.8.7. Use any sample scene except Realtime because this one works as expected.

Steps to reproduce the behavior:

  1. Open the sample project and change the platform to Android
  2. [optional] Install Android Logcat plugin if you want to see the logs.
  3. Deploy project to device
  4. On the title screen select either Chat, Assistant or Response button.
  5. Say anything or write "hello" to request OpenAI to respond.
  6. Observe that text response will be displayed and audio will be played for a second then app will crash and exit.
  7. [Optional] Check Logcat logs for more details

Expected behavior

Should not crash and work as on PC.

Screenshots

Image

Additional context

Sample crash log com.openai.unity-crash.txt

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions