Video tag can not be played in ios safari with the file stream written by itself, looking for a solution.

The

video tag reads the file stream written by java (MP4 video encoding is h264). The page code is as follows:

<video width="320" height="240" controls="controls">
    <source src="http://192.168.254.107:8080/testvideo!test.do" type="video/mp4" />
    video
</video>

the java code is as follows:

public void test() {
        try {
            File f = new File("F:/MyWorkSpace/wrote/c/cath264.mp4");
            if (!f.exists()) {
                response.sendError(404, "File not found!");
                return;
            }
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
            byte[] buf = new byte[1024];
            int len = 0;

            response.reset();
            response.setContentType("video/mp4");
            response.setHeader("Accept-Ranges", "0-" + f.length());
            response.setHeader("Content-Range",
                    "bytes " + 0 + "-" + (f.length() - 1) + "/" + f.length());
            response.setHeader("Content-Length", f.length() + "");

            OutputStream out = response.getOutputStream();
            while ((len = br.read(buf)) > 0)
                out.write(buf, 0, len);
            br.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

videos can be played on both PC and Android browsers, but not on Safari in iPhone

clipboard.png
PCSafari:


videoSafari


clipboard.png

Apr.11,2021

the following code can solve this problem, but the mobile phone will still report the error Connection reset by peer: socket write error when visiting the page. I hope there is a solution.

    public void test() {
        try {
            File f = new File("E:/cath264.mp4");
            if (!f.exists()) {
                response.sendError(404, "File not found!");
                return;
            }
            RandomAccessFile randomFile = new RandomAccessFile(f, "r");//
            long contentLength = randomFile.length();
            String range = request.getHeader("Range");
            int start = 0, end = 0;
            if(range != null && range.startsWith("bytes=")){
                String[] values = range.split("=")[1].split("-");
                start = Integer.parseInt(values[0]);
                if(values.length > 1){
                    end = Integer.parseInt(values[1]);
                }
            }
            int requestSize = 0;
            if(end != 0 && end > start){
                requestSize = end - start + 1;
            } else {
                requestSize = Integer.MAX_VALUE;
            }
     
            byte[] buffer = new byte[4096];
            response.setContentType("video/mp4");
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("ETag", "cath264");
            response.setHeader("Last-Modified", new Date().toString());
            //content length
            if(range == null){
                response.setHeader("Content-length", contentLength + "");
            }else{
                //
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);//206
                long requestStart = 0, requestEnd = 0;
                String[] ranges = range.split("=");
                if(ranges.length > 1){
                    String[] rangeDatas = ranges[1].split("-");
                    requestStart = Integer.parseInt(rangeDatas[0]);
                    if(rangeDatas.length > 1){
                        requestEnd = Integer.parseInt(rangeDatas[1]);
                    }
                }
                long length = 0;
                if(requestEnd > 0){
                    length = requestEnd - requestStart + 1;
                    response.setHeader("Content-length", "" + length);
                    response.setHeader("Content-Range", "bytes " + requestStart + "-" + requestEnd + "/" + contentLength);
                }else{
                    length = contentLength - requestStart;
                    response.setHeader("Content-length", "" + length);
                    response.setHeader("Content-Range", "bytes "+ requestStart + "-" + (contentLength - 1) + "/" + contentLength);
                }
            }
            ServletOutputStream out = response.getOutputStream();
            int needSize = requestSize;
            randomFile.seek(start);
            while(needSize > 0){
                int len = randomFile.read(buffer);
                if(needSize < buffer.length){
                    out.write(buffer, 0, needSize);
                } else {
                    out.write(buffer, 0, len);
                    if(len < buffer.length){
                        break;
                    }
                }
                needSize -= buffer.length;
            }
            randomFile.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b379-2c291.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b379-2c291.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?