Sometimes,Receiver content keeps loading and non error. [Chromecast] - android

I send the local file by NanoHTTPD in Android.
I send an URL to the Chromecast. Till the video end, it automatically send another URL to the Chromecast. Sometimes it keeps loading forever. In this situation, I send any URL to the Chromecast it will return "success", but the Chromecast still loading forever,and try to use other APP is the same situation. "REBOOT", is now the only way that can solve this problem.
MediaInfo mediaInfo = new MediaInfo.Builder(trackURL).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setContentType("video/mp4").setMetadata(metadata).setCustomData(custonObject).build();
remoteMediaPlayer.load(googleApiClient, mediaInfo, true).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
#Override
public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
if (mediaChannelResult.getStatus().isSuccess()) {
Log.d("ChromecastController", "video play!");
} else {
Log.d("ChromecastController", "video fail!");
}
}
});
http://i.imgur.com/JhW7mYf.png

Related

Google Cast Remote Display Sometimes Loses Video or Audio

My Android app uses the Remote Display API to cast video to the user's Cast-enabled device. Unfortunately, we have to use a proprietary video player, hence why I can't use the normal video API. This is sadly out of my control.
The app waits for the user to select a Display and launch a video. I would say that it works well approximately 50% of the time. Often, audio will cease to play while the video continues. Sometimes (but more rarely) the opposite happens- the Cast screen turns black while audio continues. And there is often audio and video skipping while the video plays, which isn't experienced when just viewing on the device.
I gather playing video over Remote Display isn't ideal, but I'd think audio and video should continue streaming throughout, especially since Remote Display was created for graphic-intensive games in-mind.
Also, the fact that audio stops when the activity is pushed into the background is a bit of a deal-breaker. Are there any plans to change this?
Here are some pieces of code that show how I'm creating the connection and starting video. Maybe I'm doing something dumb that causes it to perform poorly?
This code is called when the user selects a device from the MediaRouteChooserDialog:
#Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
selectedDevice = CastDevice.getFromBundle(info.getExtras());
Intent intent = new Intent(mainActivity,
ExampleMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("cast", true);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(mainActivity, 0, intent, 0);
CastRemoteDisplayLocalService.NotificationSettings settings =
new CastRemoteDisplayLocalService.NotificationSettings.Builder()
.setNotificationPendingIntent(notificationPendingIntent).build();
CastRemoteDisplayLocalService.startService(
mainActivity,
ExamplePresentationService.class,
config.getCastId(castButton.getContext()),
selectedDevice,
settings,
new CastRemoteDisplayLocalService.Callbacks() {
#Override
public void onRemoteDisplaySessionStarted(CastRemoteDisplayLocalService service) {
Log.d(TAG, "onServiceStarted");
}
#Override
public void onRemoteDisplaySessionError(Status errorReason) {
Log.d(TAG, "onServiceError: " + errorReason.getStatusCode());
}
}
);
}
My CastRemoteDisplayLocalService creates the CastPresentation in its createPresentation method:
#TargetApi(17)
private void createPresentation(Display display) {
dismissPresentation();
mPresentation = new PresentationPlayer(this, display, castHelper, adManager);
try {
mPresentation.show();
//mMediaPlayer.start();
} catch (WindowManager.InvalidDisplayException ex) {
Log.e(TAG, "Unable to show presentation, display was removed.", ex);
dismissPresentation();
}
}
And when the user selects a video, the following code is executed in CastPresentation:
public void startVideo(VideoData data) {
FrameLayout videoBase = (FrameLayout)findViewById(R.id.cast_video_frame);
videoBase.setVisibility(View.VISIBLE);
toggleLogoScreen(false);
if (player != null) {
player.stop();
player.close();
player = null;
videoBase.setVisibility(View.VISIBLE);
}
player = CvpPlayer.create(PlayerConstants.PlayerType.NEXSTREAM, castHelper.getExampleMainActivity(), videoBase);
player.setPlayerListener(this);
player.initPlayer();
}
Any ideas are greatly appreciated.

ConnectSDK Android

Working with connectSDK Android for casting videos on samsung android Tv.
I've successfully launch the default youtube app and played as well.
But when ever i'm trying to stop the currently running Youtube video getting a response code = 400 i.e
com.connectsdk.service.command.ServiceCommandError: Bad Request
Anyone worked on this? or familiar with the issue?
Code:
if (runningAppSession != null) {
if (runningAppSession.getSessionType() == LaunchSessionType.App) {
getLauncher().closeApp(runningAppSession, new ResponseListener<Object>() {
#Override
public void onError(ServiceCommandError error) {
System.out.println(error);
}
#Override
public void onSuccess(Object object) {
System.out.println(object.toString());
}
});
}
}

Cast to youtube chromecast receiver from other android app

I'm adding chromecast support to the app Narwhal TV for reddit https://play.google.com/store/apps/details?id=org.elsewhat.reddit&hl=en
As it aggregates youtube videos popular on reddit, I want to reuse the youtube chromecast receiver app.
I've made some progress already: I'm able to connect to the chromecast device and start the youtube app.
However, I am not able to start the playback of a video. Most of this progress is due to the castv2-youtube project https://github.com/xat/castv2-youtube
Start the youtube application
private String CHROMECAST_APP_ID="233637DE";
Cast.CastApi.launchApplication(mApiClient, CHROMECAST_APP_ID, false)
Define the youtube channel
class YoutubeChannel implements Cast.MessageReceivedCallback {
public String getNamespace() {
return "urn:x-cast:com.google.youtube.mdx";
}
#Override
public void onMessageReceived(CastDevice castDevice, String namespace,
String message) {
Log.d("RedditTV", "onMessageReceived: " + message);
}
}
Listen to both the media and youtube channels
Cast.CastApi.setMessageReceivedCallbacks(mApiClient,
mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer);
Cast.CastApi.setMessageReceivedCallbacks(mApiClient,
youtubeChannel.getNamespace(),
youtubeChannel);
Send message to start the video
String message = String.format(locale,"{\"type\":\"flingVideo\", \"data\":{\"videoId\":\""+redditPost.getYoutubeId()+"\" ,\"currentTime\":%.2f}}",time) ;
Log.w("RedditTV", "Chromecast message to "+ youtubeChannel.getNamespace() + " :"+message);
try {
Cast.CastApi.sendMessage(mApiClient, youtubeChannel.getNamespace(), message)
.setResultCallback(
new ResultCallback<Status>() {
#Override
public void onResult(Status result) {
if (!result.isSuccess()) {
Log.e("RedditTV", "Sending message failed");
}
}
});
} catch (Exception e) {
Log.e("RedditTV", "Exception while sending message", e);
}
The output of shows the actual message sent (I've also tried to set currentTime as 0)
Chromecast message to urn:x-cast:com.google.youtube.mdx :{"type":"flingVideo", "data":{"videoId":"4Bn2SEHsyNY" ,"currentTime":1420065958051.00}}
The message is sent successfully, but nothing happens on the chromecast side.
I don't have any good way of debugging it either.

Chromecast Android Sender RemoteMediaPlayer producing No current media session

I have been able to successfully cast video to a Chromecast and have the option let the video play when disconnecting and it all works great. However, if I choose to quit the application and let the video continue playing and then try to re-join the currently playing session and try to use the RemoteMediaPlayer to control the video I am getting: "java.lang.IllegalStateException: No current media session".
Just as a background, I am saving the route id and session id on the initial connect into preferences and am able to successfully call "Cast.CastApi.joinApplication" and when in the onResult I am recreating the Media Channel and setting the setMessageReceivedCallbacks like so:
Cast.CastApi.joinApplication(mApiClient,"xxxxxxxx",persistedSessionId).setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {
#Override
public void onResult(Cast.ApplicationConnectionResult applicationConnectionResult) {
Status status = applicationConnectionResult.getStatus();
if (status.isSuccess()) {
mRemoteMediaPlayer = new RemoteMediaPlayer();
mRemoteMediaPlayer.setOnStatusUpdatedListener(
new RemoteMediaPlayer.OnStatusUpdatedListener() {
#Override
public void onStatusUpdated() {
Log.d("----Chromecast----", "in onStatusUpdated");
}
});
mRemoteMediaPlayer.setOnMetadataUpdatedListener(
new RemoteMediaPlayer.OnMetadataUpdatedListener() {
#Override
public void onMetadataUpdated() {
Log.d("----Chromecast----", "in onMetadataUpdated");
}
});
try {
Cast.CastApi.setMessageReceivedCallbacks(mApiClient,mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer);
} catch (IOException e) {
Log.e("----Chromecast----", "Exception while creating media channel", e);
}
//-----------RESOLUTION START EDIT------------------
mRemoteMediaPlayer.requestStatus(mApiClient).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
#Override
public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
Status stat = mediaChannelResult.getStatus();
if(stat.isSuccess()){
Log.d("----Chromecast----", "mMediaPlayer getMediaStatus success");
// Enable controls
}else{
Log.d("----Chromecast----", "mMediaPlayer getMediaStatus failure");
// Disable controls and handle failure
}
}
});
//-----------RESOLUTION END EDIT------------------
}else{
Log.d("----Chromecast----", "in status failed");
}
}
}
If I declare the RemoteMediaPlayer as static:
private static RemoteMediaPlayer mRemoteMediaPlayer;
I can join the existing session as well as control the media using commands like:
mRemoteMediaPlayer.play(mApiClient);
or
mRemoteMediaPlayer.pause(mApiClient);
But once I quit the application obviously the static object is destroyed and the app produces the aforementioned "No current media session" exception. I am definitely missing something because after I join the session and register the callback perhaps I need to start the session just like it was creating when I initially loaded the media using mRemoteMediaPlayer.load(.
Can someone please help as this is very frustrating?
The media session ID is part of the internal state of the RemoteMediaPlayer object. Whenever the receiver state changes, it sends updated state information to the sender, which then causes the internal state of the RemoteMediaPlayer object to get updated.
If you disconnect from the application, then this state inside the RemoteMediaPlayer will be cleared.
When you re-establish the connection to the (still running) receiver application, you need to call RemoteMediaPlayer.requestStatus() and wait for the OnStatusUpdatedListener.onStatusUpdated() callback. This will fetch the current media status (including the current session ID) from the receiver and update the internal state of the RemoteMediaPlayer object accordingly. Once this is done, if RemoteMediaPlayer.getMediaStatus() returns non-null, then it means that there is an active media session that you can control.
As user3408864 pointed out, requestStatus() after rejoining the session works. Here is how i managed to solve it in my case and it should work in yours.
if(MAIN_ACTIVITY.isConnected()){
if(MAIN_ACTIVITY.mRemoteMediaPlayer == null){
MAIN_ACTIVITY.setRemoteMediaPlayer();
}
MAIN_ACTIVITY.mRemoteMediaPlayer.requestStatus(MAIN_ACTIVITY.mApiClient).setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
#Override
public void onResult(RemoteMediaPlayer.MediaChannelResult mediaChannelResult) {
if(playToggle ==0){
try {
MAIN_ACTIVITY.mRemoteMediaPlayer.pause(MAIN_ACTIVITY.mApiClient);
playToggle =1;
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
MAIN_ACTIVITY.mRemoteMediaPlayer.play(MAIN_ACTIVITY.mApiClient);
playToggle =0;
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
Ignore, MAIN_ACTIVITY, it is just a static reference to my activity since i run this piece of code from a Service. Also, setRemoteMediaPlayer() is a method where i create a new RemoteMediaPlayer() and attach the corresponding Listeners.
Hopefully this helps. Also, sorry if any mistake, it is my first post to StackOverFlow.

chromecast "failed to start application: no application is running" after called startSession()

I'm testing playing online video using chromecast.
After onRouteSelected(), I create the ApplicationSession and attach a MediaProtocalMessageStream;
Then I called mSession.startSession(); with no APP_ID, so I assume the build-in app inside chromecast play the video for me. This code works perfect and I can play online mp4 videos without writing my own receiver.
But, When I try to leave the video play app, I can't go back anymore, there is always an error message comes from onSessionStartFailed() which says
StartSessionTask failed with error: failed to start application: no
application is running
I don't remember how the first time I got into the video play app, which I don't leave for few day.
But I do know how I leave it, Here is what I did before I can never startSession again:
open Youtube app, get a deviced connected
play some youtube videos
disconnected from a chormecast, then the chromecast return to the starting page
So, doesn't anybody know what's going on here? How to open the build-in video app again?
By the way, My chromecast get a system update just after I return to the starting page, I don't know if google update something cause startSession() fail.
Below is the code I startSession and attach a mediaStream.
mSession = new ApplicationSession(mCastContext, mSelectedDevice);
ApplicationSession.Listener listener = new ApplicationSession.Listener() {
#Override
public void onSessionStarted(ApplicationMetadata appMetadata) {
mChannel = mSession.getChannel();
mStream = new MediaProtocolMessageStream();
mChannel.attachMessageStream(mStream);
if (mStream.getPlayerState() == null) {
ContentMetadata metaData = new ContentMetadata();
metaData.setTitle("Test Video");
String url = "http://www.auby.no/files/video_tests/h264_720p_hp_5.1_6mbps_ac3_planet.mp4";
try {
mCommand = mStream.loadMedia(url, metaData, true);
mCommand.setListener(new MediaProtocolCommand.Listener() {
#Override
public void onCompleted(MediaProtocolCommand arg0) {
onSetVolume(0.5);
}
#Override
public void onCancelled(MediaProtocolCommand arg0) {
}
});
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onSessionStartFailed(SessionError error) {
Log.d("TEST", "Session Started failed");
}
#Override
public void onSessionEnded(SessionError error) {
Log.d("TEST", "Session Started end");
}
};
mSession.setListener(listener);
try {
mSession.startSession();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You will have to use your own app id and own receiver. Google's default receiver doesn't play video streams anymore (it used to). It only handles Chrome tab mirroring now.

Categories

Resources