Android Cast Sender
July 18, 2021
- Implementing CAST Sender
- Develop Cast Sender using Android Cast API
- Setting up Meta-Data, Media-Info, RemoteMediaClient
- Add the metadata which shows on the casting dialogue
- Add the MediaInfo using the MediaInfo builder
- Create a media load request object
- Load it using RemoteMediaClient
- Using a RemoteMediaClient is similar to a MediaPlayerObject
- Check for DEVICE_CONNECTED status
- Shows Cast Playing
- Pending things that you can try
- Reference
Here Android Cast Sender may be a phone or tablet running on Android or iOS, or it may be a laptop computer running Chrome. A sender application running on the sender device uses the Google Cast API for the appropriate platform to discover and communicate with the receiver application running on the receiver device.
Implementing CAST Sender
The Google Cast SDK includes API libraries to connect to the receiver.
Steps :
- Android cast API from sender device tries to connect to Chromecast or Google Home and sends app id
- Chromecast or Google Home uses app id to get URL of HTML5 application
- Then using that URL, HTML page or Audio is rendered/streamed on device
To develop please refer below steps, that will help to quickly get started.
Develop Cast Sender using Android Cast API
Add the MediaRouteButton in xml or in the Menu Options
<androidx.mediarouter.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:mediaRouteTypes="user"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Bind the media_route_button with Casting functionality
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), mMediaRouteButton);
Enabled MediaRouteButton
OnClick Route Button Device Dialogue
Setting up Meta-Data, Media-Info, RemoteMediaClient
Add the metadata which shows on the casting dialogue
MediaMetadata metadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_GENERIC);
metadata.putString(MediaMetadata.KEY_TITLE, "Sample Music");
metadata.putString(MediaMetadata.KEY_SUBTITLE, "Sound Helix");
metadata.addImage(new WebImage(Uri.parse("https://homepages.cae.wisc.edu/~ece533/images/airplane.png")));
movieMetadata.addImage(new WebImage(Uri.parse("https://github.com/mkaflowski/HybridMediaPlayer/blob/master/images/cover.jpg?raw=true")));
Add the MediaInfo using the MediaInfo builder
MediaInfo mediaInfo = new MediaInfo.Builder("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")
.setContentType("audio/mp3")
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setMetadata(metadata)
.build();
Create a media load request object
MediaLoadRequestData mediaLoadRequestData = new MediaLoadRequestData.Builder().setMediaInfo(mediaInfo).build();
Load it using RemoteMediaClient
RemoteMediaClient remoteMediaClient = mCastContext.getSessionManager().getCurrentCastSession().getRemoteMediaClient();
Using a RemoteMediaClient is similar to a MediaPlayerObject
if (remoteMediaClient.isPlaying()) {
// Change Image button to play
mPlayPauseButton.setImageResource(R.drawable.ic_play_arrow_black_24dp);
// Pause if playing
remoteMediaClient.pause();
} else {
// Change Image button to pause
mPlayPauseButton.setImageResource(R.drawable.ic_pause_black_24dp);
if (remoteMediaClient.isPaused()) {
// Play if previous pause
remoteMediaClient.play();
} else {
// If first time playing
remoteMediaClient.load(mediaLoadRequestData);
}
}
Check for DEVICE_CONNECTED status
if (mCastContext.getCastState() == CastState.CONNECTED) {
// Load MediaInfo
} else{
Toast.makeText(getApplicationContext(),"CAST NOT CONNECTED",Toast.LENGTH_SHORT).show();
}
Shows Cast Playing
Pending things that you can try
- Show cast options in Notification
- Adding Queues as playlist
- More Cast controls and Mediaroutes
Reference
- Setup for Developing with the Cast Application Framework (CAF) for Android
- Google Sender App
- Integrate Cast Into Your Android App
- Sending media to Chromecast has never been easier (Exoplayer cast extension)
- Google Play Services: Google Cast v3 and Media
- Everything You NEED To Know About Chrome Cast and Its Development