# Getting Started with the MediaPlayer

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core MediaPlayer and highlights the major steps in the configuration of the component.

You will initialize a MediaPlayer and learn how to play the source initially by default. Then, you will see how to attach an event handler to the component. Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the component.

![Sample Telerik UI for ASP.NET Core MediaPlayer](https://www.telerik.com/aspnet-core-ui/documentation/assets/7e1f80f43d884d92d71df2f00dde9383/mediaplayer-getting-started.png)

## Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

* You can use the [Telerik REPL](https://netcorerepl.telerik.com/) playground and skip installing the components on your system and configuring a project.

* You can prepare a Visual Studio project by following the following guide:

  * Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from [a project template](https://www.telerik.com/aspnet-core-ui/documentation/vs-integration/new-project-wizard).

## 1. Prepare the CSHTML File

The first step is to add the required directives at the top of the `.cshtml` document:

* To use the Telerik UI for ASP.NET Core HtmlHelpers:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

* To use the Telerik UI for ASP.NET Core TagHelpers:

  ```cshtml
  @addTagHelper *, Kendo.Mvc
  ```

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>MediaPlayer with event handler</h4>

<p>

</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>MediaPlayer with event handler</h4>

<p>

</p>
```

## 2. Initialize the MediaPlayer

Use the MediaPlayer HtmlHelper or TagHelper to add the component to a page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the MediaPlayer element.
* The `Media()` is used to provide the component with the required media file or URL to play.
* The `AutoPlay()` configuration allows whether the video or audio should start playing on initial load.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>MediaPlayer with event handler</h4>

<p>
    @(Html.Kendo().MediaPlayer()
        .Name("mediaplayer")
        .Media(m => m
            .Title("ProgressNEXT 2019 Highlights")
            .Source("https://youtu.be/2OvvwWShNWo")
        )
    )
</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>MediaPlayer with event handler</h4>

<p>
    <kendo-mediaplayer name="mediaplayer">
    </kendo-mediaplayer>
</p>
```

## 3. Enable Initial Play

The next step is to configure the MediaPlayer to play its source automatically on initial load. You can do that by using the `AutoPlay()` configuration.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>MediaPlayer with event handler</h4>

<p>
    @(Html.Kendo().MediaPlayer()
        .Name("mediaplayer")
        .AutoPlay(true)
        .Media(m => m
            .Title("ProgressNEXT 2019 Highlights")
            .Source("https://youtu.be/2OvvwWShNWo")
        )
    )
</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>MediaPlayer with event handler</h4>

<p>
    <kendo-mediaplayer name="mediaplayer"  
           auto-play="true">
    </kendo-mediaplayer>
</p>
```

## 4. Handle a MediaPlayer Event

The MediaPlayer exposes a `Pause()` event that you can handle and assign specific functions to the component. In this tutorial, you will use the `Pause()` event to display a message when the user stops the media.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<script>
   function onPause(e) {
       console.log("Playing paused."); // displays a message in the browser console
   }
</script>

<h4>MediaPlayer with event handler</h4>

<p>
    @(Html.Kendo().MediaPlayer()
        .Name("mediaplayer")
        .AutoPlay(true)
        .Media(m => m
            .Title("ProgressNEXT 2019 Highlights")
            .Source("https://youtu.be/2OvvwWShNWo")
        )
        .Events(e => e.Pause("onPause"))
    )
</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<script>
   function onPause(e) {
       console.log("Playing paused."); // displays a message in the browser console
   }
</script>

<h4>MediaPlayer with event handler</h4>

<p>
    <kendo-mediaplayer name="mediaplayer" 
           auto-play="true" on-pause="onPause">
    </kendo-mediaplayer>
</p>
```

For more examples, refer to the [demo on using the events of the MediaPlayer](https://demos.telerik.com/aspnet-core/mediaplayer/events).

## 5. (Optional) Reference Existing MediaPlayer Instances

You can reference the MediaPlayer instances that you have created and build on top of their existing configuration:

1. Use the `id` attribute of the component instance to establish a reference.

   ```JS script
   <script>
       var mediaplayerReference = $("#mediaplayer").data("kendoMediaPlayer"); // mediaplayerReference is a reference to the existing mediaplayer instance of the helper.
   </script>
   ```

2. Use the [MediaPlayer client-side API](https://docs.telerik.com/kendo-ui/api/ui/mediaplayer#methods) to control the behavior of the widget. In this example, you will use the `mute` method to silence the media source.

   ```JS script
   <script>
       var mediaplayerReference = $("#mediaplayer").data("kendoMediaPlayer"); // mediaplayerReference is a reference to the existing timeMediaPlayer instance of the helper.
       mediaplayerReference.mute(); // Mutes the currently playing file.
   </script>
   ```

For more information on referencing specific helper instances, see the [Methods and Events](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/helper-basics/events-and-methods) article.

## Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

* [Sample code with the MediaPlayer HtmlHelper](https://netcorerepl.telerik.com/cdlvnYkL01bNFM4T40)
* [Sample code with the MediaPlayer TagHelper](https://netcorerepl.telerik.com/GxPPxaaB069F4cSu33)

## Next Steps

* [Playlists](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/media/mediaplayer/playlists)
* [Multiple Sources](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/media/mediaplayer/multiple-sources)

## See Also

* [Using the API of the MediaPlayer for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/mediaplayer/api)
* [Client-Side API of the MediaPlayer](https://docs.telerik.com/kendo-ui/api/ui/mediaplayer)
* [Server-Side API of the MediaPlayer](https://www.telerik.com/aspnet-core-ui/documentation/api/mediaplayer)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
