Skip to content

I'm noticing in v0.6.5, the first onEnded event gets fired very late #575

@dchhetri

Description

@dchhetri

I'm not sure why, perhaps i'm using it incorrectly. I have this snippet

const playAudio = (audioData: ArrayBuffer): void => {
  console.log("Playing audio chunk, size:", audioData.byteLength)
  try {
    if (!audioContext) {
      audioContext = new AudioContext({ sampleRate })
    }

    const float32Array = convert16BitPCMToFloat32(audioData)

    const audioBuffer = createAudioBuffer(float32Array)

    const source = audioContext.createBufferSource()
    source.buffer = audioBuffer
    source.connect(audioContext.destination)

    const currentTime = audioContext.currentTime
    const startTime = Math.max(currentTime, nextStartTime)
    console.log("currentTime:", currentTime, "startTime:", startTime)

    const now = Date.now()
    console.log("***AUDIO CHUNK START ", now)
    source.start(startTime)
    nextStartTime = startTime + audioBuffer.duration

    source.onEnded = () => {
      console.log("***AUDIO CHUNK END ", (Date.now() - now) / 1000, " seconds")
      lastSourceNode = null
      isPlaying = false
      _processQueue()
    }

    lastSourceNode = source
  } catch (error: any) {
    console.error("Audio playback error:", error)
  }
}

and I'm noticing for the first audio chunk (and sporadically others at times), even if the audioBuffer duration is < 200ms, the onEnded gets fired seconds later. For example one audio chunk finished after 1.79seconds even if the duration was 0.2s

I'm trying to isolate the issue (if its my code or something else) but I'm not able to find it yet. It's more noticeable on lower end Android devices.

In my system, I get a lot 10-20 audio chunks from gemini to be played so maybe some js issue on my side as well. I'm still debugging but would appreciate it anyone had ideas on if it could be something with the audio api.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions