AudioPlaybackStats: toJSON() method

The toJSON() method of the AudioPlaybackStats interface is a serializer; it returns a JSON representation of the AudioPlaybackStats object.

Syntax

js
toJSON()

Parameters

None.

Return value

A JSON object that is the serialization of the AudioPlaybackStats object.

Examples

Using the toJSON method

In this example, calling stats.toJSON() returns a JSON representation of the AudioPlaybackStats object.

js
const audioCtx = new AudioContext();
const stats = audioCtx.playbackStats;

// ...

// Log current total duration
console.log(stats.toJSON());

This would log a JSON object like so:

json
{
  "underrunDuration": 0,
  "underrunEvents": 0,
  "totalDuration": 68.252138,
  "averageLatency": 0.01863,
  "minimumLatency": 0,
  "maximumLatency": 0.018654
}

To get a JSON string, you can use JSON.stringify(stats) directly; it will call toJSON() automatically.

Specifications

Specification
Web Audio API
# dom-audioplaybackstats-tojson

Browser compatibility

See also