AudioBufferQueueSourceNode 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
}
The pitch correction algorithm introduces processing latency. As a result, when scheduling precise playback times, you should start input samples slightly ahead of the intended playback time. For more details, see getLatency().
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);
audioBufferQueue.enqueueBuffer(buffer2);
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 onBufferEnded event.
| Parameter | Type | Description |
|---|---|---|
buffer | AudioBuffer | Buffer with next data. |
Returns string.
dequeueBuffer
Removes a buffer from the queue. Note that onBufferEnded event will not be fired for buffers that were removed.
| Parameter | Type | Description |
|---|---|---|
bufferId | string | ID 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 onBufferEnded event will not be fired for buffers that were removed.
Returns undefined.
start Overridden
Schedules the AudioBufferQueueSourceNode to start playback of enqueued AudioBuffers, or starts to play immediately.
| Parameter | Type | Description |
|---|---|---|
when Optional | number | The time, in seconds, at which playback is scheduled to start. If when is less than AudioContext.currentTime or set to 0, the node starts playing immediately. Default: 0. |
offset Optional | number | The position, in seconds, within the first enqueued audio buffer where playback begins. The default value is 0, which starts playback from the beginning of the first enqueued buffer. If the offset exceeds the buffer’s duration, it’s automatically clamped to the valid range. |
pause
Stops audio immediately. Unlike stop(), which fully stops playback and clears the queued buffers,
pause() halts the audio while keeping the current playback position, allowing you to resume from the same point later.
Returns undefined.
Events
onBufferEnded
Sets (or remove) callback that will be fired when a specific buffer has ended with payload of type OnBufferEndEventType
You can remove callback by passing null.
audioBufferQueueSourceNode.onBufferEnded = (event) => { //setting callback
console.log(`buffer with id {event.bufferId} ended`);
if (event.isLastBufferInQueue) {
console.log('That was the last buffer in the queue');
}
};
Remarks
OnBufferEndEventType
Type definitions
interface OnBufferEndEventType {
bufferId: string; // the ID of the buffer that has ended
isLastBufferInQueue: boolean; // a boolean indicating whether it was the last buffer in the queue
}