android tv-Managing User Interaction

 In the live TV experience the user changes channels and is presented with channel and program information briefly before the information disappears. Other types of information, such as messages ("DO NOT ATTEMPT AT HOME"), subtitles, or ads may need to persist. As with any TV app, such information should not interfere with the program content playing on the screen.

Your TV input must render video onto a Surface object, which is passed by theTvInputService.Session.onSetSurface() method. Here's an example of how to use a MediaPlayer instance for playing content in the Surface object:

@Override
public boolean onSetSurface(Surface surface) {
    if (mPlayer != null) {
        mPlayer.setSurface(surface);
    }
    mSurface = surface;
    return true;
}

@Override
public void onSetStreamVolume(float volume) {
    if (mPlayer != null) {
        mPlayer.setVolume(volume, volume);
    }
    mVolume = volume;
}

Similarly, here's how to do it using ExoPlayer:

@Override
public boolean onSetSurface(Surface surface) {
    if (mPlayer != null) {
        mPlayer.sendMessage(mVideoRenderer,
                MediaCodecVideoTrackRenderer.MSG_SET_SURFACE,
                surface);
    }
    mSurface = surface;
    return true;
}

@Override
public void onSetStreamVolume(float volume) {
    if (mPlayer != null) {
        mPlayer.sendMessage(mAudioRenderer,
                MediaCodecAudioTrackRenderer.MSG_SET_VOLUME,
                volume);
    }
    mVolume = volume;
}

Use an overlay to display subtitles, messages, ads or MHEG-5 data broadcasts. By default, the overlay is disabled. You can enable it when you create the session by callingTvInputService.Session.setOverlayViewEnabled(true), as in the following example:

@Override
public final Session onCreateSession(String inputId) {
    BaseTvInputSessionImpl session = onCreateSessionInternal(inputId);
    session.setOverlayViewEnabled(true);
    mSessions.add(session);
    return session;
}

Use a View object for the overlay, returned from TvInputService.Session.onCreateOverlayView(), as shown here:

@Override
public View onCreateOverlayView() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.overlayview, null);
    mSubtitleView = (SubtitleView) view.findViewById(R.id.subtitles);

    // Configure the subtitle view.
    CaptionStyleCompat captionStyle;
    float captionTextSize = getCaptionFontSize();
    captionStyle = CaptionStyleCompat.createFromCaptionStyle(
            mCaptioningManager.getUserStyle());
    captionTextSize *= mCaptioningManager.getFontScale();
    mSubtitleView.setStyle(captionStyle);
    mSubtitleView.setTextSize(captionTextSize);
    return view;
}

.> When you call TvInputService.Session.onTune(), you can prevent the video from being presented by calling TvInputService.Session.notifyVideoUnavailable() and passing the VIDEO_UNAVAILABLE_REASON_TUNING constant, as shown in the following example.

@Override
public boolean onTune(Uri channelUri) {
    if (mSubtitleView != null) {
        mSubtitleView.setVisibility(View.INVISIBLE);
    }
    notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING);
    mUnblockedRatingSet.clear();

    mDbHandler.removeCallbacks(mPlayCurrentProgramRunnable);
    mPlayCurrentProgramRunnable = new PlayCurrentProgramRunnable(channelUri);
    mDbHandler.post(mPlayCurrentProgramRunnable);
    return true;
}

Then, when the content is rendered to the Surface, you callTvInputService.Session.notifyVideoAvailable() to allow the video to display, like so:

@Override
public void onDrawnToSurface(Surface surface) {
    mFirstFrameDrawn = true;
    notifyVideoAvailable();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值