Serve 2 files simultaneously with Nanohttpd from Android device

780 views Asked by At

I want to send local mp3 files from my device to Chromecast. I allready have a version of Nanohttpd running and it works well, i can play my songs on my tv without problems with:

MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);

MediaInfo mediaInfo = new MediaInfo.Builder(
 "http://192.168.0.XX:8080")
.setContentType("audio/mp3")
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setMetadata(mediaMetadata)
.build()

mRemoteMediaPlayer.load(mApiClient, mediaInfo, true) .....

...where "http://192.168.0.XX:8080" is my server url.

Now, i want to add a cover to my mediaMetadata, but, for this, i need to serve de picture file also, since this picture is sended as WebImage to Cromecast like this:

mediaMetadata.addImage(new WebImage(Uri.parse("My Url in Nanohttpd ")));

It could be possible to create a WebImage directly from resource??

If not, is any way to serve bouth (song and picture) simultaneously? Maybe I can serve the song in http: //192.168.0.XX:8080/song and the picture at http: //192.168.0.XX:8080/image or something like that, but i don't know how...

Here is my current Nanohttpd serve method:

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

        String mediasend = "audio/mp3";
        FileInputStream fis = null;
        File song = new File(songLocalPath);
        Log.e("Creando imputStream", "Size: ");
            try {
                fis = new FileInputStream(song);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        Response.Status st = Response.Status.OK;
        return new NanoHTTPD.Response(st, mediasend, fis,song.length());
    } 

Every aproach for this will be wellcome.

1

There are 1 answers

1
Juanjo On BEST ANSWER

Well, finally I change serve method to have 2 URLS and distinguish between them:

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


 if (uri.contains("picture")){          
 //serve the picture 
      return new NanoHTTPD.Response(st, mediasend, fisPicture, f.length());

 }else if (uri.contains("song")){

 //serve the song
      return new NanoHTTPD.Response(st, mediasend, fisSong, f.length());
 }

And in the Sender App, to send the song:

 MediaMetadata mediaMetadata = new       MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);

 MediaInfo mediaInfo = new MediaInfo.Builder(
 "http://192.168.0.XX:8080/song")
 .setContentType("audio/mp3")
 .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
 .setMetadata(mediaMetadata)
 .build()

 mRemoteMediaPlayer.load(mApiClient, mediaInfo, true)

and for the Album cover:

mediaMetadata.addImage(new WebImage(Uri.parse(http://192.168.0.XX:8080/picture));