3

I want to cache a stream before feeding it to Androids mediaplayer. My idea is to feed the android mediaplayer a link to nanohttpd which will then open a connection to the remote webserver and forward the stream to the mediaplayer. Meanwhile I will cache the stream, but this is not implemented yet since the first part doesn't work.

The main problem is that the mediaplayer only seems to receive the first few bytes before the stream is closed. Any idea what I'm doing wrong?

Here is the relevant code:

@Override
public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
    String stringurl = parms.get(HTTPDService.key);

    if (stringurl != null) {

    // Initialize variables
    URL url = null;
    HttpURLConnection connection = null;
    InputStream in = null;

    // Open the HTTP connection
    try {
        url = new URL(stringurl);
        connection = (HttpURLConnection) url.openConnection();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
      // Read the response.
      in = connection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }


    String datatype = connection.getContentType(); //NanoHTTPD.MIME_DEFAULT_BINARY
    return new NanoHTTPD.Response(Status.ACCEPTED, datatype, in);
}

EDIT I just found the same question (unanswered) here: NanoHTTPD. Cache InputStream to file and continue streaming

Community
  • 1
  • 1
Markus
  • 2,526
  • 4
  • 28
  • 35
  • I am also trying to do same... But, I know the reason why it is happening what is happening with what you are trying to do... When you try to open the input stream, I think it is picking up the file data which is saved until the time of opening of the input stream... It does not picks up the appended data to the file... – Atul Dravid - White Pvt. Ltd. Jan 11 '15 at 12:36

0 Answers0