Android Open Source - com.elsewhat.android.slideshow Chromecast Addin






From Project

Back to project page com.elsewhat.android.slideshow.

License

The source code is released under:

Copyright (C) 2012 Dagfinn Parnas <dagfinn.parnas@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Sof...

If you think the Android project com.elsewhat.android.slideshow listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.elsewhat.android.slideshow.activities;
/*  w  ww.  j a v  a2 s. c  om*/
import java.io.IOException;

import android.app.Activity;
import android.support.v7.app.MediaRouteButton;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.media.MediaRouter.RouteInfo;
import android.util.Log;

import com.google.cast.ApplicationChannel;
import com.google.cast.ApplicationMetadata;
import com.google.cast.ApplicationSession;
import com.google.cast.CastContext;
import com.google.cast.CastDevice;
import com.google.cast.MediaProtocolMessageStream;
import com.google.cast.MediaRouteAdapter;
import com.google.cast.MediaRouteHelper;
import com.google.cast.MediaRouteStateChangeListener;
import com.google.cast.SessionError;

public class ChromecastAddin implements MediaRouteAdapter {
    private static final String TAG = "ChromecastAddin";
  private MediaRouteButton mMediaRouteButton;
    private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private MediaRouter.Callback mMediaRouterCallback;
    private CastDevice mSelectedDevice;
    private MediaRouteStateChangeListener mRouteStateListener;
  private CastContext mCastContext;
  private ApplicationSession mSession;
    
    
  public void onCreate(Activity activity, MediaRouteButton mMediaRouteButton){

        mCastContext = new CastContext(activity.getApplicationContext());
        MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
        mMediaRouter = MediaRouter.getInstance(activity.getApplicationContext());
        mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(
                MediaRouteHelper.CATEGORY_CAST);
        mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
        mMediaRouterCallback = new MyMediaRouterCallback();    
    
  }

    private class MyMediaRouterCallback extends MediaRouter.Callback {
        @Override
        public void onRouteSelected(MediaRouter router, RouteInfo route) {
            MediaRouteHelper.requestCastDeviceForRoute(route);
        }

        @Override
        public void onRouteUnselected(MediaRouter router, RouteInfo route) {
            mSelectedDevice = null;
            mRouteStateListener = null;
        }
    }  

  @Override
  public void onDeviceAvailable(CastDevice device, String arg1,
      MediaRouteStateChangeListener listener) {
    mSelectedDevice = device;
        mRouteStateListener = listener;
        
        openSession();
  }


  public boolean isRouteAvailable(){
    return mMediaRouter.isRouteAvailable(mMediaRouteSelector, 0);
  }
  

  @Override
  public void onSetVolume(double arg0) {
    // TODO Auto-generated method stub
    
  }


  @Override
  public void onUpdateVolume(double arg0) {
    // TODO Auto-generated method stub
    
  }
  
    /**
     * Starts a new video playback session with the current CastContext and selected device.
     * https://github.com/googlecast/cast-android-sample/blob/master/src/com/example/castsample/CastSampleActivity.java
     */
    private void openSession() {
        mSession = new ApplicationSession(mCastContext, mSelectedDevice);

        // TODO: The below lines allow you to specify either that your application uses the default
        // implementations of the Notification and Lock Screens, or that you will be using your own.
        int flags = 0;

        // Comment out the below line if you are not writing your own Notification Screen.
        // flags |= ApplicationSession.FLAG_DISABLE_NOTIFICATION;

        // Comment out the below line if you are not writing your own Lock Screen.
        // flags |= ApplicationSession.FLAG_DISABLE_LOCK_SCREEN_REMOTE_CONTROL;
        mSession.setApplicationOptions(flags);

        Log.v(TAG, "Beginning session with context: " + mCastContext);
        Log.v(TAG, "The session to begin: " + mSession);
        mSession.setListener(new com.google.cast.ApplicationSession.Listener() {

            private MediaProtocolMessageStream mMessageStream;


      @Override
            public void onSessionStarted(ApplicationMetadata appMetadata) {
              Log.v(TAG, "Getting channel after session start");
                ApplicationChannel channel = mSession.getChannel();
                if (channel == null) {
                    Log.e(TAG, "channel = null");
                    return;
                }
                Log.v(TAG, "Creating and attaching Message Stream");
                mMessageStream = new MediaProtocolMessageStream();
                channel.attachMessageStream(mMessageStream);

                if (mMessageStream.getPlayerState() == null) {

                }
            }


            @Override
            public void onSessionEnded(SessionError error) {
                Log.i(TAG, "onSessionEnded " + error);
            }


      @Override
      public void onSessionStartFailed(SessionError error) {
        Log.i(TAG, "onSessionStartFailed " + error);
      }
        });

        try {
          //Log.v((TAG, "Starting session with app name " + getString(R.string.app_name));
            String appID="DUMMY";
            // TODO: To run your own copy of the receiver, you will need to set app_name in 
            // /res/strings.xml to your own appID, and then upload the provided receiver 
            // to the url that you whitelisted for your app.
            // The current value of app_name is "YOUR_APP_ID_HERE".
            mSession.startSession(appID);
        } catch (IOException e) {
            Log.e(TAG, "Failed to open session", e);
        }
    }
  
}




Java Source Code List

com.elsewhat.android.slideshow.activities.ChromecastAddin.java
com.elsewhat.android.slideshow.activities.ISlideshowInstance.java
com.elsewhat.android.slideshow.activities.SlideshowActivity.java
com.elsewhat.android.slideshow.activities.SlideshowDreamService.java
com.elsewhat.android.slideshow.activities.SlideshowPreferences.java
com.elsewhat.android.slideshow.api.Analytics.java
com.elsewhat.android.slideshow.api.AndroidUtils.java
com.elsewhat.android.slideshow.api.AsyncQueueableObject.java
com.elsewhat.android.slideshow.api.AsyncReadQueue.java
com.elsewhat.android.slideshow.api.CustomGallery.java
com.elsewhat.android.slideshow.api.DeletablePreference.java
com.elsewhat.android.slideshow.api.DownloadableObject.java
com.elsewhat.android.slideshow.api.FileDownloader.java
com.elsewhat.android.slideshow.api.FileUtils.java
com.elsewhat.android.slideshow.api.FlingKeyEvent.java
com.elsewhat.android.slideshow.api.ImageAdapter.java
com.elsewhat.android.slideshow.api.QueueablePhotoObject.java
com.elsewhat.android.slideshow.api.ReadOnlyPreference.java
com.elsewhat.android.slideshow.api.SlideshowBackend.java
com.elsewhat.android.slideshow.api.SlideshowPhotoCached.java
com.elsewhat.android.slideshow.api.SlideshowPhotoDrawable.java
com.elsewhat.android.slideshow.api.SlideshowPhoto.java
com.elsewhat.android.slideshow.backend.FlickrPublicSetBackend.java
com.elsewhat.android.slideshow.backend.OPMLBackend.java
com.elsewhat.android.slideshow.backend.SmugMugRecentBackend.java