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(options: AudioBufferBaseSourceNodeOptions)

interface AudioBufferBaseSourceNodeOptions {
pitchCorrection: boolean // specifies if pitch correction algorithm has to be available
}

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);
}

Properties

AudioBufferQueueSourceNode does not define any additional properties. It inherits all properties from AudioBufferBaseSourceNode.

Methods

It inherits all methods from AudioBufferBaseSourceNode.

enqueueBuffer

Adds 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

Removes 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 undefined.

clearBuffers

Removes all buffers from the queue. Note that onEnded event will not be fired for buffers that were removed.

Returns undefined.

Events

onEnded
Overridden

Sets (or remove) callback that will be fired when queue source node has been stopped with payload { bufferId: undefined, isLast: undefined } or when a specific buffer has ended with payload { bufferId: <bufferId>, isLast: <isLast> }, where bufferId is the ID of the buffer that has ended and isLast is a boolean indicating whether it was the last buffer in the queue.

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`);
}
};