Last active
March 22, 2017 03:41
-
-
Save Evin1-/731db243e71876923742d7ce3d2671ef to your computer and use it in GitHub Desktop.
Minimal Chromecast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| tools:context="com.example.myapplication.MainActivity"> | |
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Hello World!" /> | |
| <Button | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:onClick="doMagic" /> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.example.myapplication"> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <application> | |
| <!--...--> | |
| <!-- TODO Change this com.example.myapplication to your package name --> | |
| <meta-data | |
| android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME" | |
| android:value="com.example.myapplication.CastOptionsProvider" /> | |
| </application> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies { | |
| //... | |
| compile 'com.android.support:mediarouter-v7:25.3.0' | |
| compile 'com.googlecode.android-query:android-query:0.25.9' | |
| compile 'com.google.android.gms:play-services-cast-framework:10.2.1' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CastOptionsProvider implements OptionsProvider { | |
| private static final String APP_ID = "4F8B3483"; | |
| @Override | |
| public CastOptions getCastOptions(Context context) { | |
| return new CastOptions.Builder() | |
| .setReceiverApplicationId(APP_ID) | |
| .build(); | |
| } | |
| @Override | |
| public List<SessionProvider> getAdditionalSessionProviders(Context context) { | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends AppCompatActivity { | |
| private static final String TAG = "MainActivityTAG_"; | |
| private static final String VIDEO_TITLE = "Hello title"; | |
| private static final String VIDEO_SUBTITLE = "Hello subtitle"; | |
| private static final String VIDEO_URL = "http://techslides.com/demos/sample-videos/small.mp4"; | |
| private static final String VIDEO_TYPE = "videos/mp4"; | |
| private static final int VIDEO_DURATION = 5; | |
| private CastContext castContext; | |
| private MenuItem mediaRouteMenuItem; | |
| private CastSession castSession; | |
| private SessionManagerListener<CastSession> sessionManagerListener; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| castContext = CastContext.getSharedInstance(this); | |
| castSession = castContext.getSessionManager().getCurrentCastSession(); | |
| sessionManagerListener = generateSessionManagerListener(); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| super.onCreateOptionsMenu(menu); | |
| getMenuInflater().inflate(R.menu.browse, menu); | |
| mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.m_browse_cast); | |
| return true; | |
| } | |
| @Override | |
| protected void onResume() { | |
| castContext.getSessionManager().addSessionManagerListener(sessionManagerListener, CastSession.class); | |
| super.onResume(); | |
| } | |
| private MediaInfo buildMediaInfo() { | |
| MediaMetadata movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); | |
| movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, VIDEO_SUBTITLE); | |
| movieMetadata.putString(MediaMetadata.KEY_TITLE, VIDEO_TITLE); | |
| return new MediaInfo.Builder(VIDEO_URL) | |
| .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED) | |
| .setContentType(VIDEO_TYPE) | |
| .setMetadata(movieMetadata) | |
| .setStreamDuration(VIDEO_DURATION * 1000) | |
| .build(); | |
| } | |
| public void doMagic(View view) { | |
| if (castSession == null) { | |
| Toast.makeText(this, "Connect to Chromecast first", Toast.LENGTH_SHORT).show(); | |
| return; | |
| } | |
| RemoteMediaClient remoteMediaClient = castSession.getRemoteMediaClient(); | |
| remoteMediaClient.load(buildMediaInfo(), true, 0); | |
| } | |
| private SessionManagerListener<CastSession> generateSessionManagerListener() { | |
| return new SessionManagerListener<CastSession>() { | |
| @Override | |
| public void onSessionStarting(CastSession castSession) { | |
| } | |
| @Override | |
| public void onSessionStarted(CastSession castSession, String s) { | |
| MainActivity.this.castSession = castSession; | |
| } | |
| @Override | |
| public void onSessionStartFailed(CastSession castSession, int i) { | |
| } | |
| @Override | |
| public void onSessionEnding(CastSession castSession) { | |
| } | |
| @Override | |
| public void onSessionEnded(CastSession castSession, int i) { | |
| } | |
| @Override | |
| public void onSessionResuming(CastSession castSession, String s) { | |
| } | |
| @Override | |
| public void onSessionResumed(CastSession castSession, boolean b) { | |
| MainActivity.this.castSession = castSession; | |
| } | |
| @Override | |
| public void onSessionResumeFailed(CastSession castSession, int i) { | |
| } | |
| @Override | |
| public void onSessionSuspended(CastSession castSession, int i) { | |
| } | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment