TikTok for Developers

Developer Docs

Last updated October 2, 2026

Audio

Provide the tools to create and manage audio playback. Use InnerAudioContext for straightforward audio playback controls, and WebAudioContext for advanced audio processing, synthesis, and analysis.

tt.createInnerAudioContext()

Creates an InnerAudioContext object instance.

Returns

Returns an InnerAudioContext object instance.

Example

const innerAudioContext = tt.createInnerAudioContext();
console.log('Created audio context instance:', innerAudioContext);

tt.createWebAudioContext()

Creates a WebAudioContext object instance.

Returns

Returns a WebAudioContext object instance.

Example

const webAudioContext = tt.createWebAudioContext();
console.log('Created web audio context:', webAudioContext);

InnerAudioContext instance methods

InnerAudioContext.destroy()

Destroys the current instance.

InnerAudioContext.pause()

Pauses playback. Audio resumed after pausing will continue from the paused position.

InnerAudioContext.play()

Starts playback.

InnerAudioContext.seek(position)

Seeks to the specified position.

Parameters

Field

Type

Description

Required

position

double

The seek time in seconds

Yes

InnerAudioContext.stop()

Stops playback. Audio played after stopping will start from the beginning.

InnerAudioContext event listener methods

The following table lists the .on* event listener methods for InnerAudioContext. Each method accepts a callback parameter of type function.

Method

Description

.onCanplay(callback)

Listens for the event when audio enters a playable state. Does not guarantee smooth playback afterwards.

.onEnded(callback)

Listens for the event when audio naturally plays to the end

.onError(callback)

Listens for audio playback error events. The callback receives an object with errMsg (string) and errorCode (int32).

.onPause(callback)

Listens for audio pause events

.onPlay(callback)

Listens for audio play events

.onSeeked(callback)

Listens for the event when a seek operation completes

.onSeeking(callback)

Listens for the event when a seek operation is in progress

.onStop(callback)

Listens for audio stop events

.onTimeUpdate(callback)

Listens for audio playback progress update events

.onWaiting(callback)

Listens for audio loading events. Triggered when audio needs to stop and load due to insufficient data.

The following table lists the .off* event listener removal methods for InnerAudioContext. Each method accepts an optional callback parameter of type function. If no callback is provided, all listeners for that event are removed.

Method

Description

.offCanplay(callback)

Removes the listener for the canplay event

.offEnded(callback)

Removes the listener for the ended event

.offError(callback)

Removes the listener for the error event

.offPause(callback)

Removes the listener for the pause event

.offPlay(callback)

Removes the listener for the play event

.offSeeked(callback)

Removes the listener for the seeked event

.offSeeking(callback)

Removes the listener for the seeking event

.offStop(callback)

Removes the listener for the stop event

.offTimeUpdate(callback)

Removes the listener for the time update event

.offWaiting(callback)

Removes the listener for the waiting event

Example

const listener = function(res) { console.log(res); };

innerAudioContext.onCanplay(listener);
innerAudioContext.offCanplay(listener); // Pass the same function object used for listening

innerAudioContext.onEnded(listener);
innerAudioContext.offEnded(listener);

innerAudioContext.onError(listener);
innerAudioContext.offError(listener);

innerAudioContext.onPause(listener);
innerAudioContext.offPause(listener);

innerAudioContext.onPlay(listener);
innerAudioContext.offPlay(listener);

innerAudioContext.onSeeked(listener);
innerAudioContext.offSeeked(listener);

innerAudioContext.onSeeking(listener);
innerAudioContext.offSeeking(listener);

innerAudioContext.onStop(listener);
innerAudioContext.offStop(listener);

innerAudioContext.onTimeUpdate(listener);
innerAudioContext.offTimeUpdate(listener);

innerAudioContext.onWaiting(listener);
innerAudioContext.offWaiting(listener);

WebAudioContext instance methods

WebAudioContext.close()

Closes the WebAudioContext. Immediately releases the resources of the current context.

Returns

Returns a Promise.

Warning: Do not access the state property after calling close().

Example

const audioCtx = TTMinis.game.createWebAudioContext();
audioCtx.close().then(() => {
  // Do not access audioCtx.state after close
});

WebAudioContext.createAnalyser()

Creates an AnalyserNode. Can be used to retrieve audio time and frequency data and implement data visualization.

Returns

Returns an AnalyserNode.

Example

const audioCtx = TTMinis.game.createWebAudioContext();
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.fftSize;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);

WebAudioContext.createBiquadFilter()

Creates a BiquadFilterNode.

Returns

Returns a BiquadFilterNode.

WebAudioContext.createBuffer(numOfChannels, length, sampleRate)

Creates an AudioBuffer representing a short audio segment residing in memory.

Parameters

Field

Type

Description

Required

numOfChannels

int32

The number of audio channels in the buffer

Yes

length

int32

The number of sample frames in the buffer

Yes

sampleRate

int32

The sample rate of the audio data, in samples per second

Yes

Returns

Returns an AudioBuffer instance.

Example

const audioCtx = TTMinis.game.createWebAudioContext();
const channels = 2;
const frameCount = audioCtx.sampleRate * 2.0;
const myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);

WebAudioContext.createBufferSource()

Creates a BufferSourceNode instance to play audio data through an AudioBuffer object.

Returns

Returns a BufferSourceNode.

Example

const audioCtx = TTMinis.game.createWebAudioContext();

const loadAudio = (url) => {
  return new Promise((resolve, reject) => {
    TTMinis.game.request({
      url,
      responseType: 'arraybuffer',
      success: res => {
        console.log('res.data', res.data);
        audioCtx.decodeAudioData(res.data, buffer => {
          resolve(buffer);
        }, err => {
          console.error('decodeAudioData fail', err);
          reject(err);
        });
      },
      fail: res => {
        console.error('request fail', res);
        reject(res);
      }
    });
  });
};

const play = () => {
  loadAudio('YOUR_AUDIO_URL.mp3').then(buffer => {
    let source = audioCtx.createBufferSource();
    source.buffer = buffer;
    source.connect(audioCtx.destination);
    source.start();
  }).catch(() => {
    console.log('fail');
  });
};

play();

WebAudioContext.createChannelMerger(numberOfInputs)

Creates a ChannelMergerNode.

Parameters

Field

Type

Description

Required

numberOfInputs

int32

The number of input streams to maintain in the output stream

Yes

Returns

Returns a ChannelMergerNode.

WebAudioContext.createChannelSplitter(numberOfOutputs)

Creates a ChannelSplitterNode.

Parameters

Field

Type

Description

Required

numberOfOutputs

int32

The number of channels in the input audio stream to output separately

Yes

Returns

Returns a ChannelSplitterNode.

WebAudioContext.createDelay(maxDelayTime)

Creates a DelayNode.

Parameters

Field

Type

Description

Required

maxDelayTime

double

The maximum delay time in seconds

Yes

Returns

Returns a DelayNode.

Example

let audioCtx = TTMinis.game.createWebAudioContext();
const delayNode = audioCtx.createDelay(5);

WebAudioContext.createDynamicsCompressor()

Creates a DynamicsCompressorNode.

Returns

Returns a DynamicsCompressorNode.

Example

let audioCtx = TTMinis.game.createWebAudioContext();
let compressor = audioCtx.createDynamicsCompressor();

compressor.threshold.value = -50;
compressor.knee.value = 40;
compressor.ratio.value = 12;
compressor.attack.value = 0;
compressor.release.value = 0.25;

WebAudioContext.createGain()

Creates a GainNode.

Returns

Returns a GainNode.

WebAudioContext.createOscillator()

Creates an OscillatorNode.

Returns

Returns an OscillatorNode.

WebAudioContext.createPanner()

Creates a PannerNode.

Returns

Returns a PannerNode.

WebAudioContext.createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)

Creates a ScriptProcessorNode.

Parameters

Field

Type

Description

Required

bufferSize

int32

The buffer size in sample frames

Yes

numberOfInputChannels

int32

The number of channels for the input node

Yes

numberOfOutputChannels

int32

The number of channels for the output node

Yes

Returns

Returns a ScriptProcessorNode.

Example

let audioCtx = TTMinis.game.createWebAudioContext();
const sampleSize = 4096;
audioCtx.createScriptProcessor(sampleSize, 1, 1);

WebAudioContext.createWaveShaper()

Creates a WaveShaperNode.

Returns

Returns a WaveShaperNode.

WebAudioContext.decodeAudioData(audioData, successCallback, errorCallback)

Asynchronously decodes audio data into an AudioBuffer.

Parameters

Field

Type

Description

Required

audioData

ArrayBuffer

An ArrayBuffer containing the audio file data

Yes

successCallback

function

Called when the audio data is successfully decoded. Receives the decoded AudioBuffer as a parameter.

No

errorCallback

function

Called when the audio data fails to decode

No

Returns

Returns an AudioBuffer through the success callback.

Example

TTMinis.game.request({
  url: YOUR_AUDIO_URL,
  responseType: 'arraybuffer',
  success: res => {
    audioCtx.decodeAudioData(res.data, buffer => {
      console.log(buffer);
    }, err => {
      console.error('decodeAudioData fail', err);
    });
  }
});

WebAudioContext.resume()

Resumes a paused WebAudioContext.

Returns

Returns a Promise.

WebAudioContext.suspend()

Suspends the WebAudioContext.

Returns

Returns a Promise.

TikTok for Developers