-
Notifications
You must be signed in to change notification settings - Fork 53
Live TV Apps
The MediaPlayer class is capable of playing live media as well as media on demand, simply by providing a live media source to the class. In all respects, the MediaPlayer class behaves the same way regardless of the media type. However, there are some considerations that you need to take into account when your app is responsible for live media. This topic addresses some of these concerns and provides tips on how to deal with them.
- Adjusting the Start Time
- Managing the Buffer
- Managing the Media Duration
- Creating a Custom Time Formatter
- Updating the Media
Adjusting the Start Time
When you load a video or audio tag into the MediaPlayer class and then begin playing, the player automatically starts from the beginning of the content. When you are dealing with live media, you need to adjust the start time to correspond to the current playback time. If an event has been playing for twenty minutes when a user first turns on your app, the start time should be adjusted to twenty minutes before you begin playing the content. For an example on how to do this, see Adjusting the Start Time.
Managing the Buffer
Whenever you are streaming live content, you should try to maintain a buffer in order to provide a smooth experience for the user. When you maintain a buffer, you should have a visual representation of it on the timeline in order to show the buffer to the user. In addition, when you have a buffer it is important to be aware that the time of the media you are playing will lag a small amount behind the actual live time.
Managing the Media Duration
One of the parameters of any media associated with the MediaPlayer class is the TVJS.MediaPlayer.endTime. By default, the endTime is equivalent to the duration of the media. When you are dealing with live content, the duration is not a stable, known entity like it would be for a video on demand. If you have not customized the behavior, a MediaPlayer object will stop playback when it reaches the endTime. In order to keep your live stream playing, you should adjust the endTime.
Creating a Custom Time Formatter
Depending on the type of live media that your app is playing, the way you should display the time will vary. For example, you might want to display the actual time minus the buffer referenced earlier. Because of this, you will want to create a custom time formatter. The topic MediaPlayer.timeFormatter Property demonstrates how to create a custom time formatter and associate it with your MediaPlayer object.
Updating the Media
You should call setContentMetadata every time the media changes. With an app responsible for playing live media, this is more complex than for an app that only plays media on demand.
In general, you want to call setContentMetadata whenever you start displaying content or when the content that the user sees changes. Here are some specific cases where you should call setContentMetadata.
- On initial page load. This ensures that you cover the different ways that your app can be launched.
- When the user changes the channel.
- When one piece of content has ended and the next piece of content is about to begin.
- When the user resumes playback of live content after pausing. This is important because depending on when the playback was paused and for how long, different content may be playing once playback resumes.
- When you play an advertisement that is not part of the live media stream.