@coze/uniapp-api
Version:
Official Coze UniApp SDK for seamless AI integration into your applications | 扣子官方 UniApp SDK,助您轻松集成 AI 能力到应用中
175 lines (174 loc) • 5.47 kB
TypeScript
/**
* Audio format types supported by PcmStreamPlayer
*/
export type AudioFormat = 'pcm' | 'g711a' | 'g711u';
/**
* PcmStreamPlayer for WeChat Mini Program
* Plays audio streams received in raw PCM16, G.711a, or G.711u chunks
* @class
*/
export declare class PcmStreamPlayer {
private audioContext;
private inputSampleRate;
private outputSampleRate;
private audioQueue;
private volume;
private trackSampleOffsets;
private interruptedTrackIds;
private isInitialized;
private isProcessing;
private scriptNode;
private bufferSize;
private base64Queue;
private isProcessingQueue;
private lastAudioProcessTime;
private processingTimeThreshold;
private isPaused;
private currentBuffer;
private playbackPosition;
/**
* Default audio format
*/
private defaultFormat;
private static triggerAudio;
/**
* Creates a new PcmStreamPlayer instance
* @param {{sampleRate?: number, defaultFormat?: AudioFormat}} options
* @returns {PcmStreamPlayer}
*/
constructor({ sampleRate, defaultFormat, volume, }?: {
sampleRate?: number;
defaultFormat?: AudioFormat;
volume?: number;
});
/**
* Initialize the audio context
* @private
* @returns {boolean}
*/
private initialize;
/**
* Initialize a silent audio player to bypass iPhone silent mode
* @private
*/
private initSilentModeTrigger;
/**
* Start audio playback system
* @private
*/
private startPlayback;
/**
* Fill the output buffer with audio data from the current buffer or queue
* @private
*/
private fillOutputBuffer;
/**
* Get the next buffer from the queue and prepare it for playback
* @private
* @returns {boolean} True if a new buffer was prepared, false if queue is empty
*/
private getNextBuffer;
/**
* Pauses audio playback
*/
pause(): Promise<void>;
/**
* Resumes audio playback
*/
resume(): Promise<void>;
/**
* Toggles between play and pause states
*/
togglePlay(): Promise<void>;
/**
* Checks if audio is currently playing
* @returns {boolean}
*/
isPlaying(): boolean;
private isProcessingIdle;
/**
* Adds base64 encoded PCM data to a queue for processing
* This prevents blocking the main thread during audio processing
* @param {string} base64String - Base64 encoded PCM data
* @param {string} trackId - Track identifier
* @returns {boolean} - Success status
*/
addBase64PCM(base64String: string, trackId?: string): boolean;
/**
* Process the base64 queue when the main thread is idle
* @private
*/
private processBase64Queue;
/**
* Adds audio data to the currently playing audio stream
* @param {ArrayBuffer|Int16Array|Uint8Array} arrayBuffer
* @param {string} [trackId]
* @param {AudioFormat} [format] - Audio format: 'pcm', 'g711a', or 'g711u'
* @returns {Int16Array}
*/
add16BitPCM(arrayBuffer: ArrayBuffer | Int16Array | Uint8Array, trackId?: string, format?: AudioFormat): Int16Array;
/**
* Gets the offset (sample count) of the currently playing stream
* @param {boolean} [interrupt]
* @returns {{trackId: string|null, offset: number, currentTime: number} | null}
*/
getTrackSampleOffset(interrupt?: boolean): {
trackId: string | null;
offset: number;
currentTime: number;
} | null;
/**
* Strips the current stream and returns the sample offset of the audio
* @returns {{trackId: string|null, offset: number, currentTime: number} | null}
*/
interrupt(): {
trackId: string | null;
offset: number;
currentTime: number;
} | null;
/**
* Set the input sample rate for audio playback
* @param {number} sampleRate - The sample rate of the incoming audio data
*/
setSampleRate(sampleRate: number): void;
/**
* Set the default audio format
* @param {AudioFormat} format
*/
setDefaultFormat(format: AudioFormat): void;
/**
* Adds G.711 A-law encoded audio data to the currently playing audio stream
* @param {ArrayBuffer|Uint8Array} arrayBuffer - G.711 A-law encoded data
* @param {string} [trackId]
* @returns {Int16Array}
*/
addG711a(arrayBuffer: ArrayBuffer | Uint8Array, trackId?: string): Int16Array;
/**
* Adds G.711 μ-law encoded audio data to the currently playing audio stream
* @param {ArrayBuffer|Uint8Array} arrayBuffer - G.711 μ-law encoded data
* @param {string} [trackId]
* @returns {Int16Array}
*/
addG711u(arrayBuffer: ArrayBuffer | Uint8Array, trackId?: string): Int16Array;
/**
* Get the WebAudioContext's sample rate
* @returns {number} System sample rate
* @static
*/
static getSampleRate(): number;
/**
* Set volume level for audio playback
* @param {number} volume - Volume level from 0.0 (muted) to 1.0 (full volume)
*/
setVolume(volume: number): void;
/**
* Get current volume level
* @returns {number} Current volume level from 0.0 (muted) to 1.0 (full volume)
*/
getVolume(): number;
/**
* Cleanup static resources when the app is closed or the page is unloaded
*/
static cleanup(): void;
}
export default PcmStreamPlayer;