我想在将流提供给 Android 媒体播放器之前缓存它。我的想法是为 android mediaplayer 提供一个指向 nanohttpd 的链接,然后它将打开与远程 web 服务器的连接并将流转发到 mediaplayer。同时我将缓存流,但由于第一部分不起作用,因此尚未实现。
主要问题是媒体播放器似乎只在流关闭之前接收前几个字节。知道我做错了什么吗?
以下是相关代码:
@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);
}
编辑我刚刚在这里发现了同样的问题(未回答):NanoHTTPD。将 InputStream 缓存到文件并继续流式传输