Skip to main content

canPlay

SoundHub.canPlay(format: string): boolean;
SoundHub.getSupportedFormats(): string[];

Whether this browser can play a format, by extension. Both are static, so you can ask before you build a hub, which is usually the moment you want to know. The same two exist on an instance as hub.canPlay() and hub.getSupportedFormats().

Added in 6.2.0.

import { SoundHub } from 'soundhub';

SoundHub.canPlay('opus'); // false on older Safari
SoundHub.canPlay('.mp3'); // true, the dot is optional
SoundHub.getSupportedFormats(); // ['mp3', 'wav', 'm4a', ...]

The answer comes from the browser's own canPlayType, and "maybe" counts as yes, the same way every other player treats it. Known extensions: mp3, mpeg, opus, ogg, oga, wav, aac, caf, m4a, m4b, mp4, weba, webm, flac and dolby.

Letting the library pick

You rarely need to call this yourself. Hand loadSound a list of urls and it picks the first format the browser accepts, before anything is fetched, so the files it cannot use are never requested:

await soundHub.loadSound('theme', [
'/audio/theme.opus', // Chrome, Firefox, Edge
'/audio/theme.m4a', // Safari
]);

A url without a known extension, a signed CDN link for example, is used as is. The library would rather try and fail than refuse to load anything.

See also