MediaPlayers
On this page
An audio control bar (KlyAudioPlayer) and a video frame with a control bar (KlyVideoPlayer) that can restrict playback to a clip, to a number of plays and to a timeline that cannot be moved. The component drives the media element through a small JavaScript module once the page is interactive, and under static server rendering the control bar shows but nothing plays.
Examples
Audio player
The player has play and pause, a timeline, mute and a volume slider, and a speed button. Duration (60 seconds by default) is used until the media element reports its own. Compact draws a denser bar, Download adds a download link (DownloadFileName suggests the name), and Hide removes controls listed as MediaPlayerControl values. The audio is the 50 second file /media/sample.mp3 that this site serves.
<div class="flex w-full flex-col gap-4">
<KlyAudioPlayer Src="@DisplayDemo.AudioSrc" Title="Full extract" Duration="50" />
<KlyAudioPlayer Src="@DisplayDemo.AudioSrc" Title="Compact, with a download link" Duration="50" Compact Download DownloadFileName="extract.mp3" />
<KlyAudioPlayer Src="@DisplayDemo.AudioSrc" Title="Without volume and time" Duration="50"
Hide="@([MediaPlayerControl.Volume, MediaPlayerControl.Time])" />
</div>Clip and play limit, audio
Start and End define the clip, so the position and the length are relative to it (30 seconds here). MaxPlays caps the number of starts and Seekable false turns the timeline into a read-only progress bar. The player increments its own copy of Plays and raises PlaysChanged, so keep the value in your component with @bind-Plays. When the last play ends the player locks and raises OnLimitReached.
Plays used: 0 of 2.
<div class="flex w-full flex-col gap-3">
<KlyAudioPlayer Src="@DisplayDemo.AudioSrc" Title="Listening extract 1" Duration="50"
Start="15" End="45" MaxPlays="2" Seekable="false"
@bind-Plays="_plays" OnLimitReached="OnLimitReached" />
<p class="m-0 text-sm text-(--klyroo-text-muted)">
Plays used: @_plays of 2. @(_exhausted ? "Both plays are used." : "")
</p>
</div>
@code {
private int _plays;
private bool _exhausted;
private void OnLimitReached() => _exhausted = true;
}Video player
The video has an overlay play button and the controls of the audio player plus captions (Tracks), picture-in-picture and fullscreen, the last two shown once the browser supports them. AspectRatio is 16 / 9 by default and Poster is the image shown before it plays. The video here is a 6 second test pattern without sound, held in DisplayDemo.VideoSrc as a data URI of about 4 KB, so the example needs no file.
<div class="flex w-full max-w-xl flex-col gap-4">
<KlyVideoPlayer Src="@DisplayDemo.VideoSrc" Title="Test pattern" Duration="6" />
<KlyVideoPlayer Src="@DisplayDemo.VideoSrc" Title="Compact, without speed and fullscreen" Duration="6" Compact
Hide="@([MediaPlayerControl.Rate, MediaPlayerControl.Fullscreen])" />
</div>Clip and play limit, video
The video player takes the same Start, End, MaxPlays, Seekable and Plays parameters as the audio player. With End greater than Start the clip is that window, otherwise Duration gives the end. Plays and the locked state are kept by the component, and OnLimitReached tells your page that no plays are left.
Plays used: 0 of 1.
<div class="flex w-full max-w-xl flex-col gap-3">
<KlyVideoPlayer Src="@DisplayDemo.VideoSrc" Title="Safety briefing" Duration="6"
Start="1" End="5" MaxPlays="1" Seekable="false"
@bind-Plays="_plays" OnLimitReached="OnLimitReached" />
<p class="m-0 text-sm text-(--klyroo-text-muted)">
Plays used: @_plays of 1. @(_exhausted ? "The play is used." : "")
</p>
</div>
@code {
private int _plays;
private bool _exhausted;
private void OnLimitReached() => _exhausted = true;
}API
KlyAudioPlayer
A compact audio control bar with play and pause, a timeline, a mute button and a volume slider, a speed button and an optional download link, that can restrict playback to a clip ( Start and End ), to a number of plays ( MaxPlays ) and to a non-seekable timeline.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Autoplay |
bool |
false |
Starts playing once the player is interactive. Browsers can refuse to start a player that is not muted before the user has interacted with the page; the player then stays paused. |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Compact |
bool |
false |
Renders a denser control bar: less padding and spacing, smaller buttons and a shorter volume slider. |
Disabled |
bool |
false |
Disables the play, seek, mute and volume controls, and the methods that drive them. |
Download |
bool |
false |
Shows the download link ( false by default). The link also shows when DownloadUrl is set. |
DownloadFileName |
string |
The file name suggested to the browser by the download link. When null , the last segment of the link URL is used, or the browser picks a name for a data: or blob: URL. | |
DownloadUrl |
string |
The target of the download link. When null , Src is used. | |
Duration |
Double |
60 |
The total duration in seconds, used until the media element reports its own ( 60 by default). Set End to stop before the end of the file. |
End |
Double? |
The clip end, in seconds. It is used when it is greater than Start , otherwise Duration gives the end. | |
Hide |
IReadOnlyList |
The controls to remove from the control bar. Volume removes the mute button and the volume slider. Captions , PictureInPicture and Fullscreen are video controls that this player does not render. | |
Keyboard |
bool |
true |
Whether the focused player reacts to keys: Space or K toggles playback, M toggles mute ( true by default). Only keys pressed while the player itself has the focus count: a key on one of its buttons or sliders is left to that control. |
Loop |
bool |
false |
Sets loop on the media element. It is ignored when the player is restricted by MaxPlays or by Seekable being false . |
MaxPlays |
int |
0 |
The number of times the clip can be started ( 0 , the default, means unlimited). Once Plays reaches it and the current play ends, the player locks and shows that no plays are left. |
Muted (@bind-Muted) |
bool |
false |
Whether the player is muted. The mute button toggles it and raises MutedChanged . |
Plays (@bind-Plays) |
int |
0 |
The number of plays already used. The component increments it when a play starts and raises PlaysChanged : keep the value in your own state and pass it back, so a re-render does not lose it. |
Preload |
MediaPreload |
MediaPreload. |
How much of the clip the browser loads before the user plays it: Metadata by default. Values: None, Metadata, Auto. |
Rate (@bind-Rate) |
Double |
1 |
The playback rate shown on the speed button ( 1.0 by default). |
Rates |
IReadOnlyList |
The rates the speed button cycles through. The button is hidden when there are fewer than two (0.5, 0.75, 1, 1.25, 1.5 and 2 by default). | |
Seekable |
bool |
true |
Whether the user can move the position with the timeline. When false the timeline is a read-only progress bar ( true by default). |
Src |
string |
The URL of the media file. | |
Start |
Double |
0 |
The clip start, in seconds. Positions and durations are reported relative to it. |
Title |
string |
The accessible name of the player region. | |
Volume (@bind-Volume) |
Double |
1 |
The volume of the media element, from 0 to 1 ( 1.0 by default). The volume slider changes it and raises VolumeChanged . |
Events
| Name | Type | Description |
|---|---|---|
OnDownload |
EventCallback |
Raised when the download link is clicked. |
OnEnded |
EventCallback |
Raised when the position reaches the end of the clip. |
OnLimitReached |
EventCallback |
Raised when no plays are left: when a play ends with Plays at MaxPlays , and each time a play is requested while the player is locked (from the keyboard, a media key or TogglePlayAsync ). |
OnPause |
EventCallback |
Raised when the user pauses. |
OnPlay |
EventCallback |
Raised when playback starts or resumes. |
OnTimeUpdate |
EventCallback |
Raised with the position during playback, about four times a second, and when the user seeks. |
KlyVideoPlayer
A video frame with an overlay play button and a control bar (play and pause, timeline, mute and volume, speed, captions, picture-in-picture, fullscreen and an optional download link), that can restrict playback to a clip ( Start and End ), to a number of plays ( MaxPlays ) and to a non-seekable timeline.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
AspectRatio |
string |
"16 / 9" |
The CSS aspect-ratio of the player ( "16 / 9" by default). |
Autoplay |
bool |
false |
Starts playing once the player is interactive. Browsers can refuse to start a player that is not muted before the user has interacted with the page; the player then stays paused. |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Compact |
bool |
false |
Renders a denser control bar: less padding and spacing, smaller buttons and a shorter volume slider. |
Disabled |
bool |
false |
Disables the play, seek, mute and volume controls, and the methods that drive them. |
Download |
bool |
false |
Shows the download link ( false by default). The link also shows when DownloadUrl is set. |
DownloadFileName |
string |
The file name suggested to the browser by the download link. When null , the last segment of the link URL is used, or the browser picks a name for a data: or blob: URL. | |
DownloadUrl |
string |
The target of the download link. When null , Src is used. | |
Duration |
Double |
60 |
The total duration in seconds, used until the media element reports its own ( 60 by default). Set End to stop before the end of the file. |
End |
Double? |
The clip end, in seconds. It is used when it is greater than Start , otherwise Duration gives the end. | |
Hide |
IReadOnlyList |
The controls to remove from the control bar. Volume removes the mute button and the volume slider. | |
Keyboard |
bool |
true |
Whether the focused player reacts to keys: Space or K toggles playback, M toggles mute ( true by default). Only keys pressed while the player itself has the focus count: a key on one of its buttons or sliders is left to that control. |
Loop |
bool |
false |
Sets loop on the media element. It is ignored when the player is restricted by MaxPlays or by Seekable being false . |
MaxPlays |
int |
0 |
The number of times the clip can be started ( 0 , the default, means unlimited). Once Plays reaches it and the current play ends, the player locks and shows that no plays are left. |
Muted (@bind-Muted) |
bool |
false |
Whether the player is muted. The mute button toggles it and raises MutedChanged . |
Plays (@bind-Plays) |
int |
0 |
The number of plays already used. The component increments it when a play starts and raises PlaysChanged : keep the value in your own state and pass it back, so a re-render does not lose it. |
PlaysInline |
bool |
true |
Sets playsinline on the video element ( true by default). |
Poster |
string |
The poster image shown before the video plays. | |
Preload |
MediaPreload |
MediaPreload. |
How much of the clip the browser loads before the user plays it: Metadata by default. Values: None, Metadata, Auto. |
Rate (@bind-Rate) |
Double |
1 |
The playback rate shown on the speed button ( 1.0 by default). |
Rates |
IReadOnlyList |
The rates the speed button cycles through. The button is hidden when there are fewer than two (0.5, 0.75, 1, 1.25, 1.5 and 2 by default). | |
Seekable |
bool |
true |
Whether the user can move the position with the timeline. When false the timeline is a read-only progress bar ( true by default). |
Src |
string |
The URL of the media file. | |
Start |
Double |
0 |
The clip start, in seconds. Positions and durations are reported relative to it. |
Title |
string |
The accessible name of the player region. | |
Tracks |
IReadOnlyList |
The caption tracks, rendered as track elements. The track marked Default is shown first; the CC button then cycles through the tracks and off, showing the selected track and disabling the others. The choice is kept across renders and starts over when Src changes. | |
Volume (@bind-Volume) |
Double |
1 |
The volume of the media element, from 0 to 1 ( 1.0 by default). The volume slider changes it and raises VolumeChanged . |
Events
| Name | Type | Description |
|---|---|---|
OnDownload |
EventCallback |
Raised when the download link is clicked. |
OnEnded |
EventCallback |
Raised when the position reaches the end of the clip. |
OnLimitReached |
EventCallback |
Raised when no plays are left: when a play ends with Plays at MaxPlays , and each time a play is requested while the player is locked (from the keyboard, a media key or TogglePlayAsync ). |
OnPause |
EventCallback |
Raised when the user pauses. |
OnPlay |
EventCallback |
Raised when playback starts or resumes. |
OnTimeUpdate |
EventCallback |
Raised with the position during playback, about four times a second, and when the user seeks. |