AudioPlaybackStats: toJSON() method
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{APIRef("Web Audio API")}}{{SeeCompatTable}}
The **toJSON()** method of the {{domxref("AudioPlaybackStats")}} interface returns a JSON-serializable plain object representing the AudioPlaybackStats object.
The method is automatically called by {{jsxref("JSON.stringify()")}} when an AudioPlaybackStats object is encountered during JSON serialization.
Syntax
toJSON()Parameters
None.
Return value
A JSON-serializable plain object containing the following properties:
- {{domxref("AudioPlaybackStats/underrunDuration", "underrunDuration")}}
- {{domxref("AudioPlaybackStats/underrunEvents", "underrunEvents")}}
- {{domxref("AudioPlaybackStats/totalDuration", "totalDuration")}}
- {{domxref("AudioPlaybackStats/averageLatency", "averageLatency")}}
- {{domxref("AudioPlaybackStats/minimumLatency", "minimumLatency")}}
- {{domxref("AudioPlaybackStats/maximumLatency", "maximumLatency")}}
Each property's value is copied as-is.
Examples
Calling toJSON() directly
This example obtains an AudioPlaybackStats object from an audio context. Calling toJSON() directly returns a plain object. Its properties have the same values as the corresponding properties of the original object.
const audioCtx = new AudioContext();
const stats = audioCtx.playbackStats;
// ...
const json = stats.toJSON();
console.log(json); // A plain object
console.log(typeof json); // "object"
console.log(json.underrunEvents); // Same value as stats.underrunEventsSerializing to a JSON string
The AudioPlaybackStats object from the previous example is serialized using {{jsxref("JSON.stringify()")}}, which calls the toJSON() method automatically.
console.log(JSON.stringify(stats));This would log a JSON string like so (formatted for readability):
{
"underrunDuration": 0.125,
"underrunEvents": 1,
"totalDuration": 68.252138,
"averageLatency": 0.01863,
"minimumLatency": 0.01,
"maximumLatency": 0.018654
}Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- {{jsxref("JSON")}}
- Web Audio API