Skip to main content

AudioBufferQueueSourceNode

caution

Mobile only.

The AudioBufferQueueSourceNode is an AudioBufferBaseSourceNode which represents player that consists of many short buffers.

Constructor

BaseAudioContext.createBufferQueueSource()

Example

import React, { useRef } from 'react';
import {
AudioContext,
AudioBufferQueueSourceNode,
} from 'react-native-audio-api';

function App() {
const audioContextRef = useRef<AudioContext | null>(null);
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}
const audioBufferQueue = audioContextRef.current.createBufferQueueSource();
const buffer1 = ...; // Load your audio buffer here
const buffer2 = ...; // Load another audio buffer if needed
audioBufferQueue.enqueueBuffer(buffer1, false);
audioBufferQueue.enqueueBuffer(buffer2, true); // Last buffer should be marked as is
audioBufferQueue.connect(audioContextRef.current.destination);
audioBufferQueue.start(audioContextRef.current.currentTime);
}

Methods

enqueueBuffer

The above method lets you add another buffer to queue. Returns bufferId that can be used to identify the buffer in onEnded event.

ParametersTypeDescription
bufferAudioBufferBuffer with next data.

Returns string.

dequeueBuffer

The above method lets you remove a buffer from the queue. Note that onEnded event will not be fired for buffers that were removed.

ParametersTypeDescription
bufferIdstringID of the buffer to remove from the queue. It should be valid id provided by enqueueBuffer method.

Returns void.

clearBuffers

The above method lets you remove all buffers from the queue. Note that onEnded event will not be fired for buffers that were removed.

Returns void.

Events

onEnded
Overridden

Allows to set (or remove) callback that will be fired when queue source node has stopped playing with payload event.bufferId = undefined or when a specific buffer has ended with payload event.bufferId = <bufferId>.

You can remove callback by passing null.

audioBufferSourceNode.onEnded = (event) => { //setting callback
if (event.bufferId === undefined) {
console.log('queue source node has been stopped');
} else {
console.log(`buffer with id {event.bufferId} ended`);
}
};