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