使用TIF Companion Library开发TvInputService:Overlay无法显示且加载转圈问题
问题背景
我正尝试使用TIF Companion Library开发一个最简版TvInputService,以显示Overlay。Live Channels应用已识别我在EpgSyncJobService子类中声明的「Simple Channel」频道,我的Program也已在该应用的EPG中显示为当前播出节目,但界面仅显示蓝色加载spinner,仿佛频道未完成「调台」。请问我哪里出错了?
我的代码
public class MyTvInputService extends BaseTvInputService { @Override public final BaseTvInputService.Session onCreateSession(String inputId) { BaseTvInputService.Session session = new MyTvInputSession(this, inputId); session.setOverlayViewEnabled(true); return super.sessionCreated(session); } class MyTvInputSession extends BaseTvInputService.Session { public MyTvInputSession(Context context, String inputId) { super(context, inputId); } @Override public View onCreateOverlayView() { mTextView = new TextView(MyTvInputService.this); mTextView.setText("This is an example overlay"); mTextView.setTextColor(Color.RED); mTextView.setVisibility(View.VISIBLE); mTextView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return mTextView; } @Override public boolean onPlayProgram(Program program, long startPosMs) { return true; } @Override public boolean onPlayRecordedProgram(RecordedProgram recordedProgram) { return true; } @Override public void onSetCaptionEnabled(boolean enable) {} @Override public TvPlayer getTvPlayer() { return null; } } private TextView mTextView; }
问题排查与解决方案
这个问题的核心是Live Channels需要明确收到你的会话已完成加载、内容就绪的通知——你现在的代码里只是在onPlayProgram返回了true,但系统并不知道内容已经准备好,所以一直停留在加载状态。
你需要做这几个关键修改:
1. 通知系统内容已就绪
在onPlayProgram方法中,添加notifyVideoAvailable()调用,告诉Live Channels频道已经可以展示内容了:
@Override public boolean onPlayProgram(Program program, long startPosMs) { // 关键:通知系统内容已准备完毕,结束加载状态 notifyVideoAvailable(); return true; }
2. 修正TextView的作用域问题
你把mTextView定义在外部的MyTvInputService类中,当多个会话实例创建时会出现变量冲突,建议把它移到MyTvInputSession内部,同时使用getContext()获取上下文更规范:
class MyTvInputSession extends BaseTvInputService.Session { // 把TextView移到内部类中 private TextView mTextView; public MyTvInputSession(Context context, String inputId) { super(context, inputId); } @Override public View onCreateOverlayView() { mTextView = new TextView(getContext()); mTextView.setText("This is an example overlay"); mTextView.setTextColor(Color.RED); mTextView.setVisibility(View.VISIBLE); mTextView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); return mTextView; } // 其他方法保持不变 }
补充说明
对于纯Overlay的TvInputService,getTvPlayer()返回null是没问题的——因为你不需要实际的视频播放能力。但如果后续要支持视频内容,这里需要返回一个有效的TvPlayer实例。
另外,如果后续需要处理缓冲、暂停等状态,还可以使用notifyVideoBufferingStart()、notifyVideoPaused()等方法来同步UI状态。
内容的提问来源于stack exchange,提问作者mkrasowski

