【问题标题】:nanohttpd serve video, how to make it support video forward and backward(make server support http byte range)nanohttpd 服务视频,如何使其支持视频向前和向后(使服务器支持 http 字节范围)
【发布时间】:2020-06-24 08:56:04
【问题描述】:

这是我的代码:

public class VideoServer extends NanoHTTPD {
    public VideoServer() {
        super(8080);
    }

    @Override
    public Response serve(IHTTPSession session) {
        return responseVideoStream(session, "/home/roroco/Dropbox/video/t.mp4");
    }

    public Response responseVideoStream(IHTTPSession session, String videopath) {
        try {
            FileInputStream fis = new FileInputStream(videopath);
            return newChunkedResponse(Response.Status.OK, "video/mp4", fis);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

视频投放成功,但不能前进和后退

看我下面的 gif,当我点击时间进度时,视频不能倒退

【问题讨论】:

    标签: java android


    【解决方案1】:

    我从https://stackoverflow.com/a/23620420/6011193 找到解决方案,请参阅它的#getPartialResponse

    这是我的最终代码:

    public class VideoServer extends NanoHTTPD {
        String filePath = "/home/roroco/Dropbox/video/t.mp4";
    
        public VideoServer() {
            super(8080);
    
        }
    
        @Override
        public Response serve(IHTTPSession session) {
            String range = null;
            Map<String, String> headers = session.getHeaders();
            for (String key : headers.keySet()) {
                if ("range".equals(key)) {
                    range = headers.get(key);
                    break;
                }
            }
            if (range != null) {
                try {
                    return getPartialResponse("video/mp4", range);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return responseVideoStream(session, filePath);
            }
        }
    
        public Response responseVideoStream(IHTTPSession session, String videopath) {
            try {
                FileInputStream fis = new FileInputStream(videopath);
                return newChunkedResponse(Response.Status.OK, "video/mp4", fis);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    
        private Response getPartialResponse(String mimeType, String rangeHeader) throws IOException {
            File file = new File(filePath);
            String rangeValue = rangeHeader.trim().substring("bytes=".length());
            long fileLength = file.length();
            long start, end;
            if (rangeValue.startsWith("-")) {
                end = fileLength - 1;
                start = fileLength - 1
                        - Long.parseLong(rangeValue.substring("-".length()));
            } else {
                String[] range = rangeValue.split("-");
                start = Long.parseLong(range[0]);
                end = range.length > 1 ? Long.parseLong(range[1])
                        : fileLength - 1;
            }
            if (end > fileLength - 1) {
                end = fileLength - 1;
            }
            if (start <= end) {
                long contentLength = end - start + 1;
                FileInputStream fileInputStream = new FileInputStream(file);
                //noinspection ResultOfMethodCallIgnored
                fileInputStream.skip(start);
                Response response = newFixedLengthResponse(Response.Status.PARTIAL_CONTENT, mimeType, fileInputStream, contentLength);
                response.addHeader("Content-Length", contentLength + "");
                response.addHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileLength);
                response.addHeader("Content-Type", mimeType);
                return response;
            } else {
                return newChunkedResponse(Response.Status.INTERNAL_ERROR, "text/html", null);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多