
Invalid header range coming from jwplayer used in android webview
I am using, jwplayer in android webview and when I play videos from player I can see invalid range is coming, so for in valid range I am using following code to server videos in jersey,
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException,
WebApplicationException {
@SuppressWarnings("resource")
final FileChannel inputChannel = new FileInputStream(file).getChannel();
final WritableByteChannel outputChannel = Channels.newChannel(output);
try {
inputChannel.transferTo(0, inputChannel.size(), outputChannel);
} finally {
// closing the channels
inputChannel.close();
outputChannel.close();
}
}
};
MimetypesFileTypeMap typeMap = new MimetypesFileTypeMap();
String mt = typeMap.getContentType(file);
if(fileName.contains(".mp4")) {
mt = "video/mp4";
}
return Response.ok(output, mt).header(HttpHeaders.CONTENT_LENGTH, file.length()).build();
And for valid range, I am using following code to serve videos . . . .
final int chunk_size = 1024 * 1024; // 1MB chunks
String[] ranges = range.split("=")[1].split("-");
final int from = Integer.parseInt(ranges[0]);
/**
* Chunk media if the range upper bound is unspecified. Chrome sends "bytes=0-"
*/
int to = chunk_size + from;
if (to >= file.length()) {
to = (int) (file.length() - 1);
}
if (ranges.length == 2) {
to = Integer.parseInt(ranges[1]);
}
final String responseRange = String.format("bytes %d-%d/%d", from, to, file.length());
final RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(from);
final int len = to - from + 1;
final MediaStreamer streamer = new MediaStreamer(len, raf);
MimetypesFileTypeMap typeMap = new MimetypesFileTypeMap();
String mt = typeMap.getContentType(file);
if(fileName.contains(".mp4")) {
mt = "video/mp4";
}
Response.ResponseBuilder res = Response.ok(streamer, mt).status(206)
.header("Accept-Ranges", "bytes")
.header("Content-Range", responseRange)
.header(HttpHeaders.CONTENT_LENGTH, streamer.getLenth())
.header(HttpHeaders.LAST_MODIFIED, new Date(file.lastModified()));
return res.build();
But still I am not able to run videos from android webview, on ios and desktop browsers all videos work fine.
What should I do here . . . . I don't think thats a encoding issue . . . .