ChromeCast Sender Android Application Tutorial (How to Create a chrome Cast application)
Code Source -> Source
Follwing Questions can be answerd in this tutorial
How to get default cast player ID
How to use Cast button on another place then top bar
Easy to Use Cast
Requirements
Just need cast Companion library and place it in same directry as your project
https://github.com/googlecast/CastCompanionLibrary-android
Step1:
Make a new android studio Project
Open Settings File and place follwoing line of code in it
include ':CastCompanionLibrary-android'
project(':CastCompanionLibrary-android').projectDir = new File(rootDir, '../CastCompanionLibrary-android')
Step 2:
Open androidManifiest file and place following code inside it
Permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
CastCompanion Services and activities in manifiest
-> Just chnage the name of parent in below code
<receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
</intent-filter>
</service>
<service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService"/>
<activity
android:name="com.google.android.libraries.cast.companionlibrary.cast.player.VideoCastControllerActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:parentActivityName="com.example.tahir.castapplication.CastActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tahir.castapplication.CastActivity" />
<intent
</intent-filter>
</activity>
Step 3:
Create a new class and Extends it to Application
Paste followings lines in onCreate of Application Class
public void onCreate() {
super.onCreate();
VideoCastManager.
initialize(this, CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID, null, null).
setVolumeStep(0.5).
enableFeatures(VideoCastManager.FEATURE_NOTIFICATION |
VideoCastManager.FEATURE_LOCKSCREEN |
VideoCastManager.FEATURE_WIFI_RECONNECT |
VideoCastManager.FEATURE_CAPTIONS_PREFERENCE)
;
}
This is the line use to get Default media Player ID in Receiver
CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID
Open Your Activity XML file and place following code for the button
<android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:mediaRouteTypes="user"
/>
If you want to show button in Menu Bar , Paste follwing code in menu file
<item
android:id="@+id/media_route_menu_item"
android:title="Cast"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always"/>
Step4:
Now open Activity class to register Cll backs
onCreate
mCastManager = VideoCastManager.getInstance();
// -- Adding MiniController
mMini = (MiniController) findViewById(R.id.miniController);
mCastManager.addMiniController(mMini);
mCastManager.addMediaRouterButton(mediaRoutebtn);
mCastConsumer = new VideoCastConsumerImpl() {
@Override
public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) {
Toast.makeText(CastActivity.this,"Successfully Connected to Receiver Application", Toast.LENGTH_LONG).show();
super.onApplicationConnected(appMetadata, sessionId, wasLaunched);
}
@Override
public void onFailed(int resourceId, int statusCode) {
}
@Override
public void onConnectionSuspended(int cause) {
}
@Override
public void onConnectivityRecovered() {
}
@Override
public void onCastDeviceDetected(final MediaRouter.RouteInfo info) {
}
@Override
public void onConnectionFailed(ConnectionResult result) {
}
};
mCastManager.reconnectSessionIfPossible(20);
Line of code to add button on anywhere
mCastManager.addMediaRouterButton(mediaRoutebtn);
OnResume:
protected void onResume() {
super.onResume();
mCastManager = VideoCastManager.getInstance();
mCastManager.addVideoCastConsumer(mCastConsumer);
mCastManager.incrementUiCounter();
}
OnPause
protected void onPause() {
mCastManager.decrementUiCounter();
mCastManager.removeVideoCastConsumer(mCastConsumer);
super.onPause();
}
onDestroy
super.onDestroy();
if (null != mCastManager) {
mMini.removeOnMiniControllerChangedListener(mCastManager);
if(mMini != null) {
mCastManager.removeMiniController(mMini);
}
}
}
Code to add button in onOptionItems
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_cast, menu);
mediaRouteMenuItem = mCastManager.
addMediaRouterButton(menu, R.id.media_route_menu_item);
return true;
}
Now you need to have a function which will start casting any mp4 on you device
private void startCast(){
MediaMetadata mediaMetadata = new MediaMetadata( MediaMetadata.MEDIA_TYPE_MOVIE );
mediaMetadata.putString( MediaMetadata.KEY_TITLE, "Main Title");
mediaMetadata.putString( MediaMetadata.KEY_SUBTITLE, "Sub Title");
mediaMetadata.addImage(new WebImage(Uri.parse(""))); // Cast Icon
mediaMetadata.addImage(new WebImage(Uri.parse(""))); // Wallper
MediaInfo mediaInfo = new MediaInfo.Builder(urlField.getText().toString())
.setContentType("video/mp4") //for live -> "application/vnd.apple.mpegurl"
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED ) // Repplace for live -> MediaInfo.STREAM_TYPE_LIVE
.setMetadata(mediaMetadata)
.build();
Bundle bundles = com.google.android.libraries.cast.companionlibrary.utils.Utils.mediaInfoToBundle(mediaInfo);
mCastManager.startVideoCastControllerActivity(CastActivity.this,bundles,0,true);
}
If you have any Question let me know,
Source is also attached with the tutorial Code Source