Introduction
In this article, we are going to learn about the different events in Kendo Media Player. To explain how to work with events in Kendo Media Player, I’m going to use the application that was developed in my previous article, Create a video playlist using kendo media player.
Events in Kendo Media Player
There are different kinds of events in Kendo Media Player, which are listed below.
- Ready
- Play
- Pause
- Time Change
- Volume Change
- End
Let’s look at these events one by one.
Before going through these events, please go through my previous article, where I have written about how to work with Kendo Media Player
- Ready
This event is fired when the loading is over and Kendo Media Player is ready to start playing the video.
MediaPlayer.html - > This file is updated from my previous work
- <!DOCTYPE html>
- <html <head>
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css" />
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
-
- <body>
- <div id="example">
- <h3 style="font-style:italic"> My Playlist</h3> <br/> <br/>
- <div class="demo-section k-content wide" style="width: 925px;">
- <div id="mediaplayer" style="height:360px; width:640px"></div>
- <div class="k-list-container playlist">
- <ul id="listView" class="k-list"></ul>
- </div>
- </div>
- <script type="text/javascript">
- $(document).ready(function() {
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- ready: onReady,
- });
- var videos = new kendo.data.DataSource({
- transport: {
- read: {
- url: "api/Media/GetMediaPlayerlist",
- dataType: "json"
- }
- },
- });
- var listView = $("#listView").kendoListView({
- dataSource: videos,
- selectable: true,
- scrollable: true,
- template: kendo.template($("#template").html()),
- change: onChange,
- dataBound: onDataBound
- });
-
- function onChange() {
- var index = this.select().index();
- var dataItem = this.dataSource.view()[index];
- $("#mediaplayer").data("kendoMediaPlayer").media(dataItem);
- }
-
- function onDataBound() {
- this.select(this.element.children().first());
- }
-
- function onReady() {
- console.log("Video is Ready")
- }
- });
- </script>
- <script type="text/x-kendo-template" id="template">
- <li class="k-item k-state-default" onmouseover="$(this).addClass('k-state-hover')" onmouseout="$(this).removeClass('k-state-hover')"> <span>
- <img src="#:poster#" />
- <h5>#:title#</h5>
- </span> </li>
- </script>
- <style>
- .k-mediaplayer {
- float: left;
- }
-
- .playlist {
- float: left;
- width: 280px;
- height: 360px;
- overflow: auto;
- }
-
- .playlist ul,
- .playlist li {
- list-style-type: none;
- margin: 0;
- padding: 0;
- }
-
- .playlist .k-item {
- border-bottom-style: solid;
- border-bottom-width: 1px;
- padding: 14px 15px;
- }
-
- .playlist .k-item:last-child {
- border-bottom-width: 0;
- }
-
- .playlist span {
- cursor: pointer;
- display: block;
- overflow: hidden;
- text-decoration: none;
- }
-
- .playlist span img {
- border: 0 none;
- display: block;
- height: 56px;
- object-fit: cover;
- width: 100px;
- float: left;
- }
-
- .playlist h5 {
- display: block;
- font-weight: normal;
- margin: 0;
- overflow: hidden;
- padding-left: 10px;
- text-align: left;
- }
- </style>
- </div>
- </body>
-
- </html>
Result
- Play
This event fires when the video begins to play.
Snippet
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- play: onPlay,
- });
-
- function onPlay() {
- console.log("On Play event fired")
- }
Result
- Pause
This event fires when the video is paused.
Snippet
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- pause: onPause,
- });
-
- function onPause() {
- console.log("Pause Event Fired")
- }
Result
- Time Change
This event fires when the user changes the time.
Snippet
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- timeChange: ontimeChange,
- });
-
- function ontimeChange() {
- console.log("Time change")
- }
Result
- Volume Change
This event fires when the user changes the volume level.
Snippet
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- volumeChange: onVolumeChange,
- });
-
- function onVolumeChange() {
- console.log("Volume Change")
- }
Result
- End
This event fires when the media finishes playing.
Snippet
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- end: onEnd,
- });
-
- function onEnd() {
- console.log("The End of Video");
- }
Result
Complete code
MediaPlayer.html
- <!DOCTYPE html>
- <html>
-
- <head>
- <style>
- html {
- font-size: 14px;
- font-family: Arial, Helvetica, sans-serif;
- }
- </style>
- <title></title>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css" />
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
-
- <body>
- <div id="example">
- <h3 style="font-style:italic"> My Playlist</h3> <br/> <br/>
- <div class="demo-section k-content wide" style="width: 925px;">
- <div id="mediaplayer" style="height:360px; width:640px"></div>
- <div class="k-list-container playlist">
- <ul id="listView" class="k-list"></ul>
- </div>
- </div>
- <script type="text/javascript">
- $(document).ready(function() {
- $("#mediaplayer").kendoMediaPlayer({
- autoPlay: true,
- play: onPlay,
- pause: onPause,
- timeChange: ontimeChange,
- volumeChange: onVolumeChange,
- end: onEnd,
- ready: onReady
- });
- var videos = new kendo.data.DataSource({
- transport: {
- read: {
- url: "api/Media/GetMediaPlayerlist",
- dataType: "json"
- }
- },
- });
- var listView = $("#listView").kendoListView({
- dataSource: videos,
- selectable: true,
- scrollable: true,
- template: kendo.template($("#template").html()),
- change: onChange,
- dataBound: onDataBound
- });
-
- function onChange() {
- var index = this.select().index();
- var dataItem = this.dataSource.view()[index];
- $("#mediaplayer").data("kendoMediaPlayer").media(dataItem);
- }
-
- function onDataBound() {
- this.select(this.element.children().first());
- }
-
- function onPlay() {
- console.log("On Play event fired")
- }
-
- function onPause() {
- console.log("Pause Event Fired")
- }
-
- function ontimeChange() {
- console.log("Time change")
- }
-
- function onVolumeChange() {
- console.log("Volume Change")
- }
-
- function onEnd() {
- console.log("The End of Video");
- }
-
- function onReady() {
- console.log("Video is Ready")
- }
- });
- </script>
- <script type="text/x-kendo-template" id="template">
- <li class="k-item k-state-default" onmouseover="$(this).addClass('k-state-hover')" onmouseout="$(this).removeClass('k-state-hover')"> <span>
- <img src="#:poster#" />
- <h5>#:title#</h5>
- </span> </li>
- </script>
- <style>
- .k-mediaplayer {
- float: left;
- }
-
- .playlist {
- float: left;
- width: 280px;
- height: 360px;
- overflow: auto;
- }
-
- .playlist ul,
- .playlist li {
- list-style-type: none;
- margin: 0;
- padding: 0;
- }
-
- .playlist .k-item {
- border-bottom-style: solid;
- border-bottom-width: 1px;
- padding: 14px 15px;
- }
-
- .playlist .k-item:last-child {
- border-bottom-width: 0;
- }
-
- .playlist span {
- cursor: pointer;
- display: block;
- overflow: hidden;
- text-decoration: none;
- }
-
- .playlist span img {
- border: 0 none;
- display: block;
- height: 56px;
- object-fit: cover;
- width: 100px;
- float: left;
- }
-
- .playlist h5 {
- display: block;
- font-weight: normal;
- margin: 0;
- overflow: hidden;
- padding-left: 10px;
- text-align: left;
- }
- </style>
- </div>
- </body>
-
- </html>
References http://docs.telerik.com/kendo-ui/controls/media/mediaplayer/overview
http://docs.telerik.com/kendo-ui/api/javascript/ui/mediaplayer#events-end Conclusion
We have seen how to work with the events in Kendo Media Player, which are useful whenever we need to do some external/internal operations in our Application, while the event is fired .
I hope, you have enjoyed this article. Your valuable feedback, question or comments about this article are always welcome.