Search code examples
androidchromecastplaylist

Creating playlist from sd card and sending it to Chromecast


I am developing an application to send playlist to Chromecast. I want to create playlist from local media (video, images). My application will get all the videos and images from specific folder and it will create a playlist. This playlist will be send to Chromecast.

How to achieve this, I have created NanoHTTPD server on my mobile. And serving video from there. But this server serves only one video at a time.

I want serve specific folder from my local server and then want to create playlist of all the videos and images stored in that folder.

This is my NanoHTTPD :-

  public class WebServer extends NanoHTTPD 

{

public WebServer() throws IOException 
{
    super(8089);
}

@Override
public Response serve(String uri, Method method,
        Map<String, String> header, Map<String, String> parameters,
        Map<String, String> files)
{
    String answer = "";

    FileInputStream fis = null;
    String path;
    String fileOutput="";
    int counter;

    try 
    {
                    path = Environment.getExternalStorageDirectory() + "/DCIM/Video/bbb.mp4";
                    fis = new FileInputStream(path);
    } 
    catch (Exception e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Status.OK, "video/mp4", fis);
}
}

This is how i am casting video :-

private void startVideo()
{
    MediaMetadata mediaMetadata = new MediaMetadata( MediaMetadata.MEDIA_TYPE_MOVIE );
    mediaMetadata.putString( MediaMetadata.KEY_TITLE, getString( R.string.video_title ) );

    String videoURL = Configuration.video_url; 

    MediaInfo mediaInfo = new MediaInfo.Builder( videoURL )
    .setContentType( getString( R.string.content_type_mp4 ) )
    .setStreamType( MediaInfo.STREAM_TYPE_BUFFERED )
    .setMetadata( mediaMetadata )
    .build();

    Toast.makeText(getApplicationContext(), "Video build " + videoURL, Toast.LENGTH_LONG).show();

    try 
    {
        mRemoteMediaPlayer.load( mApiClient, mediaInfo, true )
        .setResultCallback( new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() 
                {
            @Override
            public void onResult( RemoteMediaPlayer.MediaChannelResult mediaChannelResult )
            {
                if( mediaChannelResult.getStatus().isSuccess() ) 
                {
                    mVideoIsLoaded = true;
                    mButton.setText( getString( R.string.pause_video ) );

                    Toast.makeText(getApplicationContext(), "Media loaded successfully", Toast.LENGTH_LONG).show();
                }
            }
                } );
    } 
    catch (IllegalStateException e) 
    {
        Toast.makeText(getApplicationContext(), "Problem occurred with media during loading : " + e, Toast.LENGTH_LONG).show();
    } 
    catch (Exception e)
    {
        Toast.makeText(getApplicationContext(), "Problem occurred with media during loading : " + e, Toast.LENGTH_LONG).show();
    }
}

Please help me, I unable to understand what to do.


Solution

  • You need to:

    • Configure your local server to serve files by their name, i.e. if I want to get a file named "video1.mp4", I would need to use "http://.../video1.mp4". This allows having multiple files accessible when needed.
    • You need to create a queue using the appropriate apis: create MediaQueueItems, one for each video in your playlist, and then put them in an array and load that using the RemoteMediaPlayer#queueLoad() method. You may want to set autoplay=true on them.

    You can look at the CastVidoes-android app for an example; the only difference would be that urls in your case for videos are pointing to your embedded server.