Skip to content

How to: Add a Basic Media Player Using Code

Scott Miller edited this page Mar 17, 2016 · 2 revisions

This document explains how to add a basic media player to a blank application by using the MediaPlayer class. If you follow these steps, the media player will be added through code as part of the initialization process. These steps can be used for either video or audio media. When you follow these steps, the media player will not automatically begin playback; instead, you must interact with it in order to play the video.

When you create a MediaPlayer object through code, you have the option to create the video or audio tag at a different time from when you create the MediaPlayer. This is not possible if you create the MediaPlayer declaratively. For more information about simultaneously creating the tag and the MediaPlayer, see How to: Add a Basic Media Player Declaratively.

To add a MediaPlayer, in your HTML file, add a div element to host the MediaPlayer:

<div id="myMediaPlayer">
<video src="http://www.contoso.com/video.mp4"></video>
</div>

In your JavaScript file, call the MediaPlayer constructor:

var mediaPlayer = new TVJS.MediaPlayer(document.getElementById("myMediaPlayer"));