video标签用自己写的文件流在ios的safari里播不了

覃雨泽 发布于 2018/08/15 10:15
阅读 1K+
收藏 0

video标签读的是java写的文件流(MP4视频编码是h264),页面代码如下:

	<video width="320" height="240" controls="controls">
  		<source src="http://192.168.254.107:8080/testvideo!test.do" type="video/mp4" />
  		您的浏览器不支持video标签
  	</video>

java代码如下:

	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();
		}
	}

视频在PC端和安卓手机的浏览器中均可以播放,但是在iPhone中的Safari上放不了。

PC端运行时系统也不会报错,在Safari上运行时会报以下错误:

Caused by: java.net.SocketException: Connection reset by peer: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
	at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
	at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
	at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
	at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
	at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
	at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:84)
	at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
	at org.apache.coyote.Response.doWrite(Response.java:517)
	at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:408)
	... 54 more

如果把视频放到工程里,video标签直接引用,视频在Safari里是可以播放的

	<video width="320" height="240" controls="controls">
  		<source src="${path}/jsp/reception/cath264.mp4" type="video/mp4" />
  		您的浏览器不支持video标签
  	</video>

左边的视频是引用的文件流,右边是直接引的视频

求大神帮忙解决一下,谢谢!

加载中
0
椒盐可乐

ios的系统是不允许直接请求整个视频文件的,是分段请求的,大文件分块下载

覃雨泽
可以了,谢谢!
0
慢慢成长
慢慢成长

你这样,我建议你走nginx静态资源处理得了;不要走java程序处理了;

这样你src改为 http://xxx/resources/test.mp4 就可以直接访问了;这样不会有任何问题;

location /resources/ {
    alias /data/xxx/videoes/; #你存放视频文件的目录
    autoindex on;
}

 

覃雨泽
这个方法可以,上边大哥的方法也可以,他先答的,就采纳他的了,不好意思了,十分感谢!
返回顶部
顶部