我仍然试图通过这个html 5播放视频&带码头的音频无错误地工作。对于大型视频,我注意到视频播放高达75%(只是一个估计值),然后跳到最后。这种情况从未发生在较短的视频中,但在视频上持续发生的时间超过10分钟左右。我没有在日志或浏览器调试器窗口中看到任何错误。以下是范围请求以及记录的几个错误但我认为不相关。
视频
Status Code:206 Partial Content
Accept-Ranges:bytes
Content-Length:16001618
Content-Range:bytes 0-16001617/16001618
Content-Type:video/webm
Server:Jetty(9.3.z-SNAPSHOT)
REQUEST
Connection:keep-alive
Range:bytes=0-
音频
Status Code:206 Partial Content
Accept-Ranges:bytes
Content-Length:9044858
Content-Range:bytes 0-9044857/9044858
Content-Type:audio/wav
REQUEST
Range:bytes=0-
视频
Status Code:206 Partial Content
Accept-Ranges:bytes
Content-Length:10834
Content-Range:bytes 15990784-16001617/16001618
Content-Type:video/webm
REQUEST
Connection:keep-alive
Range:bytes=15990784-
视频
Status Code:206 Partial Content
Accept-Ranges:bytes
Content-Length:12069458
Content-Range:bytes 3932160-16001617/16001618
Content-Type:video/webm
REQUEST
Connection:keep-alive
Range:bytes=3932160-
就像我说的那样,对于较大的视频,这种情况一直发生,所以我不认为视频已损坏。但是不会返回任何错误,除了以下内容,根据发生此错误的时间不会停止视频播放:
Accept-Ranges: bytes
Content-Range: bytes 0-11229438/11229439
Content-Length: 11229439
Content-Type: video/webm
, Content-Length=11229439, ScreenID=117435}: {1},
com.service.,java.nio.channels.ClosedChannelException
at
org.eclipse.jetty.util.IteratingCallback.close(IteratingCallback.java:427)
at org.eclipse.jetty.server.HttpConnection.onClose(HttpConnection.java:489)
处理请求的代码:
if(parameters.containsKey(Constants.PARAM_CONTENT_LENGTH)) {
totalLength = java.lang.Math.toIntExact((long)
parameters.get(Constants.PARAM_CONTENT_LENGTH));
} else {
String range =
parameters.get(Constants.PARAM_RANGE_REQUEST).toString();
String[] ranges = range.split("=")[1].split("-");
from = Integer.parseInt(ranges[0]);
if(from >= 0 && from < totalLength) {
to = (totalLength - 1);
if (ranges.length == 2) {
to = Integer.parseInt(ranges[1]);
if (to >= totalLength) {
to = (int) (totalLength - 1);
}
}
} else {
to = -1;
}
}
}
return new ContentRange(from, to, totalLength);
写入流的代码:
BufferedOutputStream bout = new BufferedOutputStream (outputStream);
Buffer b = new Buffer();
long skipped =0;
int byteLength = 0;
do {
inputStream.read(b);
int len = b.getLength();
byte[] data = (byte[]) b.getData();
int offset = b.getOffset();
if (len > 0) {
if(skipped < from) {
if(skipped + len <= from) {
skipped += len;
} else {
offset += (from - skipped);
len -= (from - skipped);
skipped = from;
}
}
if(to >= 0) {
if(from + byteLength + len <= to) {
} else {
len = (to + 1) - (from + byteLength);
}
}
byteLength+= len;
bytesWrittenObj.bytesWritten = byteLength;
bout.write(data, offset, len);
}
} while (!b.isEOM());
bout.flush();