Cast image (photo) to Chromecast

2019-06-16 16:48发布

I'm following these (1, 2) guides to create a sender Android application for Chromecast and I'm only interested in sending pictures. There are a lot of informaton and samples how to cast Text, Audio and Video. But not a single word how to that with Pictures.

I belive in power of stackoferflow and someone should've faced such problem. Please give some good sample or tutorial. All I need is guide to cast fullscreen picture using Media Router and its features.

Thats how I was sending text message using custom channel:

 /**
 * Send a text message to the receiver
 */
private void sendMessage(String message) {
    if (mApiClient != null && mSmartBusChannel != null) {
        try {
            Cast.CastApi.sendMessage(mApiClient,
                    mSmartBusChannel.getNamespace(), message)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status result) {
                            if (!result.isSuccess()) {
                                Log.e(TAG, "Sending message failed");
                            }
                        }
                    });
        } catch (Exception e) {
            Log.e(TAG, "Exception while sending message", e);
        }
    } else {
        Toast.makeText(this, message, Toast.LENGTH_SHORT)
                .show();
    }
}

Video is sending using RemotePlaybackClient.. Okay, what's about pictures?

Much thanks for any help.


EDIT:

I have found out method (on this blog) of how it is possible to send pictures from local storage. And yeah, that doesn't seem really working.

public final void openPhotoOnChromecast(String title, String url, String ownerName, String description) {
    try {
        Log.d(TAG, "openPhotoOnChromecast: " + url);
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, "viewphoto");
        payload.put("fullsizeUrl", url);
        payload.put("ownerName", ownerName);
        payload.put("title", title);
        payload.put("description", description);

        sendMessage(payload);
    } catch (JSONException e) {
        Log.e(TAG, "Cannot parse or serialize data for openPhotoOnChromecast", e);
    } catch (IOException e) {
        Log.e(TAG, "Unable to send openPhotoOnChromecast message", e);
    } catch (IllegalStateException e) {
        Log.e(TAG, "Message Stream is not attached", e);
    }
}

P.S. this method uses sendMessage(...) from these libraries (from gradle):

compile files('libs/commons-io-2.4.jar')
compile files('libs/GoogleCastSdkAndroid.jar')

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-16 17:22

Looking here: Examples using CastCompanionLibrary to simply display an image There are really three options for sending images to a Chromecast.

  1. You can encode the image in a base64 string and send it over a data channel to the receiver. If it is too big, you can split it up and send it across in multiple messages. This is a really poor use of the cast technology and really you shouldn't do this, but it is possible.
  2. You could simply send a url to the Chromecast device and grab it from your sever inside the receiver app. This the the recommended way to send photos across to the Chromecast
  3. If you aren't downloading your images from a server you could set up your own server running inside your client Android app and send a url to the receiver to grab it from there. This is rather complicated for sending images across, but is a far more robust option than option 1.

The goal of Chromecast, according to Google, is to stream content from the cloud, which is why there isn't really any native support for sending local images. Developers should be encouraged to load images on the receiver application from a server.

查看更多
登录 后发表回答