android中一个简单的chromecast示例?

时间:2014-09-08 19:45:34

标签: android chromecast television

我最近开始研究API Chromecast并设法制作一个简单的连接示例。但是,我仍然无法为电视发送一个helloword。有没有人有一个简单的例子?

这是我的代码:

public class CastTest extends ActionBarActivity{

    private static final String TAG = CastTest.class
            .getSimpleName();

    private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private MediaRouter.Callback mMediaRouterCallback;
    private CastDevice mSelectedDevice;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cast);

        mMediaRouter = MediaRouter.getInstance(getApplicationContext());
        // Create a MediaRouteSelector for the type of routes your app supports
        mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(
                        CastMediaControlIntent.categoryForCast("794B7BBF")).build();
        // Create a MediaRouter callback for discovery events
        mMediaRouterCallback = new MyMediaRouterCallback();

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(CastTest.this,
                "Teste", Toast.LENGTH_LONG).show();
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Add the callback to start device discovery
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    }

    @Override
    protected void onPause() {
        // Remove the callback to stop device discovery
        mMediaRouter.removeCallback(mMediaRouterCallback);
        super.onPause();
    }
    //Adicionar as opções para o usuário escolher 
        //Por exemplo: um chromecast ou um dispositivo wireless
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
        MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat
                .getActionProvider(mediaRouteMenuItem);
        // Set the MediaRouteActionProvider selector for device discovery.
        mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
        return true;
    }

    private class MyMediaRouterCallback extends MediaRouter.Callback {

        @Override
        public void onRouteSelected(MediaRouter router, RouteInfo info) {
            Log.d(TAG, "onRouteSelected");
            // Handle route selection.
            mSelectedDevice = CastDevice.getFromBundle(info.getExtras());

            // Just display a message for now; In a real app this would be the
            // hook  to connect to the selected device and launch the receiver 
            // app
            Toast.makeText(CastTest.this,
                "Este aplicativo não vai lançar uma aplicação", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onRouteUnselected(MediaRouter router, RouteInfo info) {
            Log.d(TAG, "onRouteUnselected: info=" + info);
            mSelectedDevice = null;
        }
    }
    public void voltar(View v) {
        Intent i = new Intent();
        i = new Intent(this, Main.class);
        startActivity(i);
        this.finish();
    }    
}

1 个答案:

答案 0 :(得分:1)

请看一下这个示例应用:https://github.com/googlecast/CastHelloText-android