Skip to content

Class: KMedia ​

Generic media from the channel's library: given a file (sound, video, or image) and playback options, it internally instantiates the right concrete player (KAudio, KVideo, or KImage) and delegates playback, stopping, volume, and positioning to it.

Remarks ​

In practice you don't instantiate KMedia yourself: you go through k.medias.playMedia(media, options), which creates a KMedia and plays it. Every method returns this (or a promise of this) and is therefore chainable.

Example ​

ts
// Play a media from the library, at 80% volume, in the top-left corner
const file = await k.medias.get('mon-alerte');
if (file) {
    await k.medias.playMedia({ file }, {
        volume: 80,
        position: { x: 0, y: 0, w: 640, h: 360 },
    });
}

See ​

KAudio, KVideo, KImage for the concrete players.

Implements ​

Properties ​

media ​

ts
media: 
  | KImage
  | KAudio
  | KVideo;

Concrete player instantiated according to the file type (sounds → KAudio, videos → KVideo, images → KImage), or undefined if no file was provided.


media_data ​

ts
media_data: MediaData;

Media data (source file + playback settings) provided at construction time.


options ​

ts
options: MediaOptions;

Resolved playback options (volume, position, effects, timeline).

Methods ​

play() ​

ts
play(): Promise<KMedia>;

Preloads if needed then starts playing the media, without waiting for it to finish.

Returns ​

Promise<KMedia>

A promise resolved with this KMedia as soon as playback has started.

Example ​

ts
const media = await k.medias.playMedia({ file });
// the code keeps running while the media plays

See ​

playSync to wait for playback to finish.

Implementation of ​

KMedia.play


playSync() ​

ts
playSync(): Promise<KMedia>;

Starts playback and waits for it to finish before resolving.

Returns ​

Promise<KMedia>

A promise resolved with this KMedia once the media has finished.

Example ​

ts
// Play a sound then continue only after it ends
await media.playSync();
console.log('the media has finished');

See ​

play to avoid waiting for the end.

Implementation of ​

KMedia.playSync


preload() ​

ts
preload(): Promise<KMedia>;

Preloads the media (download / decoding) without playing it, for a later latency-free playback.

Returns ​

Promise<KMedia>

A promise resolved with this KMedia (chainable).

See ​

play to preload and play in one go.

Implementation of ​

KMedia.preload


remove() ​

ts
remove(): KMedia;

Stops playback then permanently removes the media (audio disconnection / removal of the element from the DOM).

Returns ​

KMedia

This KMedia (chainable).

Implementation of ​

KMedia.remove


setPosition() ​

ts
setPosition(position): KMedia;

Repositions and resizes the media on screen. Has no effect on a sound.

Parameters ​

position ​

MediaPositionCreate

New position / size — see MediaPositionCreate (omitted fields keep their current value).

Returns ​

KMedia

This KMedia (chainable).

Implementation of ​

KMedia.setPosition


setVolume() ​

ts
setVolume(volume): KMedia;

Sets the playback volume. Has no effect on an image.

Parameters ​

volume ​

number

Target volume, from 0 to 100.

Returns ​

KMedia

This KMedia (chainable).

Implementation of ​

KMedia.setVolume


stop() ​

ts
stop(): KMedia;

Immediately stops the current playback (without removing the media from the DOM).

Returns ​

KMedia

This KMedia (chainable).

See ​

remove to stop and remove the media.

Implementation of ​

KMedia.stop