Search code examples
androidslideshowchromecastgoogle-castnanohttpd

How to do a slideshow with Chromecast


I have a folder with several images in my sdcard that I want to send to my TV using Chromecast as a slideshow. I am using nanoHTTPD to serve those images and everytime I click my 'next button' I serve the next image of my list to the server. An example of the ip address I get is: 192.168.X.XX:8080. I am trying to refresh this ip address so when I click the mentioned 'next button' I can display the next image in the TV. The problem is that I always see the same image but if I disconnect and connect my receiver again, the image displayed is the correct one.

@Override
    public Response serve(String uri, Method method, Map<String, String> header,Map<String, String> parameters, Map<String, String> files) {
        String mediasend = "image/jpeg";
        FileInputStream fis = null;
        String root1 = Environment.getExternalStorageDirectory().toString() + "/saved_images";
        //Log.d("Files", "Path: " + root1);
        File f = new File(root1);
        File file[] = f.listFiles();
        Log.d("Files", "Size: " + file.length);
        for (int i = 0; i < file.length; i++) {
            Log.d("Files", "FileName:" + file[i].getName());

                try {
                    fis = new FileInputStream(Environment.getExternalStorageDirectory()
                            + "/saved_images" + "/" + file[page_number].getName());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

        }

            return new NanoHTTPD.Response(etsiit.etsiitcast_def.NanoHTTPD.Response.Status.OK, mediasend, fis);


    }

}

Where page_numberincreases everytime I click the 'next button'.

    private void startSlideShow() {
    MediaMetadata mediaMetadata = new MediaMetadata( MediaMetadata.MEDIA_TYPE_MOVIE);
    mediaMetadata.putString(MediaMetadata.KEY_TITLE, getString( R.string.titulo_video ) );
    String dir_ip = wifiIpAddress();
    MediaInfo mediaInfo = new MediaInfo.Builder(dir_ip)
            .setContentType( "image/jpeg" )
            .setStreamType( MediaInfo.STREAM_TYPE_BUFFERED )
            .setMetadata( mediaMetadata )
            .build();
    try {
        mRemoteMediaPlayer.load( apiClient, mediaInfo, true )
                .setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
                    @Override
                    public void onResult( RemoteMediaPlayer.MediaChannelResult mediaChannelResult ) {
                        if( mediaChannelResult.getStatus().isSuccess() ) {
                            mVideoIsLoaded = true;
                        }
                    }
                } );
    } catch( Exception e ) {
    }
}

I am using the wifiIpAddressto get the ip address:

public String wifiIpAddress () {

    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    ipdevice=String.format("http://%d.%d.%d.%d:8080",(ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));

    return ipdevice;

}

Solution

  • Receiver is caching the image since the URL you are passing for all images is the same so you need to force it to reload the image and realize that it is a new one. Usually folks add a timestamp or something like that to the url to trick the browser that it is a new resource; you can do the same and also try to use no-cache pragma and see if that helps but the first one should work.