在直播电视体验中,用户更改频道,并短暂显示频道和节目信息,然后信息消失。其他类型的信息,例如消息(“切勿在家尝试”)、字幕或广告可能需要持续存在。与任何电视应用一样,此类信息不应干扰屏幕上播放的节目内容。
还要考虑是否应呈现某些节目内容,具体取决于内容的评级和家长控制设置,以及您的应用在内容被阻止或不可用时的行为和告知用户的方式。本课程介绍如何针对这些考虑因素开发电视输入的用户体验。
试用 电视输入服务 示例应用。
将播放器与 Surface 集成
您的电视输入必须将视频渲染到 Surface
对象上,该对象由 TvInputService.Session.onSetSurface()
方法传递。以下是如何在 Surface
对象中使用 MediaPlayer
实例播放内容的示例
Kotlin
override fun onSetSurface(surface: Surface?): Boolean { player?.setSurface(surface) mSurface = surface return true } override fun onSetStreamVolume(volume: Float) { player?.setVolume(volume, volume) mVolume = volume }
Java
@Override public boolean onSetSurface(Surface surface) { if (player != null) { player.setSurface(surface); } mSurface = surface; return true; } @Override public void onSetStreamVolume(float volume) { if (player != null) { player.setVolume(volume, volume); } mVolume = volume; }
类似地,以下是使用 ExoPlayer 的方法
Kotlin
override fun onSetSurface(surface: Surface?): Boolean { player?.createMessage(videoRenderer)?.apply { type = MSG_SET_SURFACE payload = surface send() } mSurface = surface return true } override fun onSetStreamVolume(volume: Float) { player?.createMessage(audioRenderer)?.apply { type = MSG_SET_VOLUME payload = volume send() } mVolume = volume }
Java
@Override public boolean onSetSurface(@Nullable Surface surface) { if (player != null) { player.createMessage(videoRenderer) .setType(MSG_SET_SURFACE) .setPayload(surface) .send(); } mSurface = surface; return true; } @Override public void onSetStreamVolume(float volume) { if (player != null) { player.createMessage(videoRenderer) .setType(MSG_SET_VOLUME) .setPayload(volume) .send(); } mVolume = volume; }
使用叠加层
使用叠加层显示字幕、消息、广告或 MHEG-5 数据广播。默认情况下,叠加层处于禁用状态。您可以在创建会话时通过调用 TvInputService.Session.setOverlayViewEnabled(true)
来启用它,如下例所示
Kotlin
override fun onCreateSession(inputId: String): Session = onCreateSessionInternal(inputId).apply { setOverlayViewEnabled(true) sessions.add(this) }
Java
@Override public final Session onCreateSession(String inputId) { BaseTvInputSessionImpl session = onCreateSessionInternal(inputId); session.setOverlayViewEnabled(true); sessions.add(session); return session; }
使用 View
对象作为叠加层,该对象由 TvInputService.Session.onCreateOverlayView()
返回,如下所示
Kotlin
override fun onCreateOverlayView(): View = (context.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater).run { inflate(R.layout.overlayview, null).apply { subtitleView = findViewById<SubtitleView>(R.id.subtitles).apply { // Configure the subtitle view. val captionStyle: CaptionStyleCompat = CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle) setStyle(captionStyle) setFractionalTextSize(captioningManager.fontScale) } } }
Java
@Override public View onCreateOverlayView() { LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.overlayview, null); subtitleView = (SubtitleView) view.findViewById(R.id.subtitles); // Configure the subtitle view. CaptionStyleCompat captionStyle; captionStyle = CaptionStyleCompat.createFromCaptionStyle( captioningManager.getUserStyle()); subtitleView.setStyle(captionStyle); subtitleView.setFractionalTextSize(captioningManager.fontScale); return view; }
叠加层的布局定义可能如下所示
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.exoplayer.text.SubtitleView android:id="@+id/subtitles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="32dp" android:visibility="invisible"/> </FrameLayout>
控制内容
当用户选择频道时,您的电视输入会处理onTune()
回调,该回调位于TvInputService.Session
对象中。系统电视应用的家长控制功能会根据内容分级来决定显示什么内容。以下部分介绍如何使用与系统电视应用通信的TvInputService.Session
notify
方法来管理频道和节目选择。
使视频不可用
当用户更改频道时,您需要确保在您的电视输入渲染内容之前,屏幕不会显示任何杂散视频伪像。当您调用TvInputService.Session.onTune()
时,您可以通过调用TvInputService.Session.notifyVideoUnavailable()
并传递VIDEO_UNAVAILABLE_REASON_TUNING
常量来阻止视频显示,如下例所示。
Kotlin
override fun onTune(channelUri: Uri): Boolean { subtitleView?.visibility = View.INVISIBLE notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING) unblockedRatingSet.clear() dbHandler.apply { removeCallbacks(playCurrentProgramRunnable) playCurrentProgramRunnable = PlayCurrentProgramRunnable(channelUri) post(playCurrentProgramRunnable) } return true }
Java
@Override public boolean onTune(Uri channelUri) { if (subtitleView != null) { subtitleView.setVisibility(View.INVISIBLE); } notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING); unblockedRatingSet.clear(); dbHandler.removeCallbacks(playCurrentProgramRunnable); playCurrentProgramRunnable = new PlayCurrentProgramRunnable(channelUri); dbHandler.post(playCurrentProgramRunnable); return true; }
然后,当内容渲染到Surface
时,您调用TvInputService.Session.notifyVideoAvailable()
以允许显示视频,如下所示
Kotlin
fun onRenderedFirstFrame(surface:Surface) { firstFrameDrawn = true notifyVideoAvailable() }
Java
@Override public void onRenderedFirstFrame(Surface surface) { firstFrameDrawn = true; notifyVideoAvailable(); }
此转换仅持续几分之一秒,但显示空白屏幕比允许图片闪烁奇特的闪烁和抖动在视觉上更好。
另请参阅将播放器与Surface集成,以获取有关使用Surface
渲染视频的更多信息。
提供家长控制
要确定给定内容是否被家长控制和内容分级阻止,您可以检查TvInputManager
类的isParentalControlsEnabled()
和isRatingBlocked(android.media.tv.TvContentRating)
方法。您可能还需要确保内容的TvContentRating
包含在一组当前允许的内容分级中。以下示例显示了这些考虑因素。
Kotlin
private fun checkContentBlockNeeded() { currentContentRating?.also { rating -> if (!tvInputManager.isParentalControlsEnabled || !tvInputManager.isRatingBlocked(rating) || unblockedRatingSet.contains(rating)) { // Content rating is changed so we don't need to block anymore. // Unblock content here explicitly to resume playback. unblockContent(null) return } } lastBlockedRating = currentContentRating player?.run { // Children restricted content might be blocked by TV app as well, // but TIF should do its best not to show any single frame of blocked content. releasePlayer() } notifyContentBlocked(currentContentRating) }
Java
private void checkContentBlockNeeded() { if (currentContentRating == null || !tvInputManager.isParentalControlsEnabled() || !tvInputManager.isRatingBlocked(currentContentRating) || unblockedRatingSet.contains(currentContentRating)) { // Content rating is changed so we don't need to block anymore. // Unblock content here explicitly to resume playback. unblockContent(null); return; } lastBlockedRating = currentContentRating; if (player != null) { // Children restricted content might be blocked by TV app as well, // but TIF should do its best not to show any single frame of blocked content. releasePlayer(); } notifyContentBlocked(currentContentRating); }
确定内容是否应被阻止后,通过调用TvInputService.Session
方法notifyContentAllowed()
或notifyContentBlocked()
来通知系统电视应用,如前面的示例所示。
使用TvContentRating
类使用TvContentRating.createRating()
方法生成COLUMN_CONTENT_RATING
的系统定义字符串,如下所示
Kotlin
val rating = TvContentRating.createRating( "com.android.tv", "US_TV", "US_TV_PG", "US_TV_D", "US_TV_L" )
Java
TvContentRating rating = TvContentRating.createRating( "com.android.tv", "US_TV", "US_TV_PG", "US_TV_D", "US_TV_L");
处理轨道选择
TvTrackInfo
类包含有关媒体轨道的信息,例如轨道类型(视频、音频或字幕)等等。
您的电视输入会话第一次能够获取轨道信息时,应使用所有轨道的列表调用TvInputService.Session.notifyTracksChanged()
来更新系统电视应用。当轨道信息发生更改时,再次调用notifyTracksChanged()
来更新系统。
如果对于给定的轨道类型有多个轨道可用(例如,不同语言的字幕),则系统电视应用会为用户提供一个界面来选择特定轨道。您的电视输入通过调用notifyTrackSelected()
来响应来自系统电视应用的onSelectTrack()
调用,如下例所示。请注意,当传递null
作为轨道 ID 时,这将取消选择轨道。
Kotlin
override fun onSelectTrack(type: Int, trackId: String?): Boolean = mPlayer?.let { player -> if (type == TvTrackInfo.TYPE_SUBTITLE) { if (!captionEnabled && trackId != null) return false selectedSubtitleTrackId = trackId subtitleView.visibility = if (trackId == null) View.INVISIBLE else View.VISIBLE } player.trackInfo.indexOfFirst { it.trackType == type }.let { trackIndex -> if( trackIndex >= 0) { player.selectTrack(trackIndex) notifyTrackSelected(type, trackId) true } else false } } ?: false
Java
@Override public boolean onSelectTrack(int type, String trackId) { if (player != null) { if (type == TvTrackInfo.TYPE_SUBTITLE) { if (!captionEnabled && trackId != null) { return false; } selectedSubtitleTrackId = trackId; if (trackId == null) { subtitleView.setVisibility(View.INVISIBLE); } } int trackIndex = -1; MediaPlayer.TrackInfo[] trackInfos = player.getTrackInfo(); for (int index = 0; index < trackInfos.length; index++) { MediaPlayer.TrackInfo trackInfo = trackInfos[index]; if (trackInfo.getTrackType() == type) { trackIndex = index; break; } } if (trackIndex >= 0) { player.selectTrack(trackIndex); notifyTrackSelected(type, trackId); return true; } } return false; }