Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement TvView on Android TV?

Goal

To create a reduced TvView that displays on top of a WebView.
This is specifically for live TV.
(I've seen numerous solutions that address streaming content, but this is intended for video that's delivered through the coaxial cable.)

Layout

Where I'm At Now

The WebView portion works fine, and I've learned that a RelativeLayout is required in order to layer views.
Also, TvView doesn't seem to be recognized, so I'm guessing that I'll probably need to use a SurfaceView instead.

activity_main.xml Code example

MainActivity.java enter image description here

Questions

I've seen the TvInput, example applications, but it's extremely excessive. I'm not trying to recreate the TvInput service, just leverage on existing framework.

  • Is it possible to simply call on the existing, TV service that's already on the Android device, and display it in a view?
  • If it is possible, how is it accomplished?
  • If it's not possible, what's the simplest method of implementing it? (repository links would be great.)

I've searched all over for answers but can't seem to find anything.
Thank you in advance for any assistance.


Code in Text Format

As requested by tung.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_browse_fragment"
    android:name="com.company.app.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.company.app.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TvView
        android:id="@+id/TvView"
        android:layout_width="816dp"
        android:layout_height="404dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp" />

</RelativeLayout >

MainActivity.java

package com.company.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.media.tv.TvView;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;

public class MainActivity extends Activity {
    private WebView mWebView;
    private TvView mTvView;
    private WebSettings mWebSettings;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mWebView = new WebView(this);
        mWebSettings = mWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mWebView.loadUrl("http://10.28.98.150:1000/");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        this.setContentView(mWebView);
        mTvView = new TvView(this );
        mTvView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        this.setContentView(mTvView);
    }

    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
like image 450
Dustin Halstead Avatar asked Nov 07 '22 08:11

Dustin Halstead


1 Answers

    String channelUrl="content://android.media.tv/channel/"+channelNumber;
        mTvView.tune(mInputId, Uri.parse(channelUrl));

input id is available in TV database(refering to which source channel is coming from).

If you pass these info to tvview it will start playing.

like image 184
yadunath.narayanan Avatar answered Nov 14 '22 22:11

yadunath.narayanan