今天遇到了個神奇的問題,視頻文件在pc端和安卓手機上播放都沒問題,但是在ios上就是播放不了,大概代碼如下:
前端代碼:
<video id="video" width="350" height="500" controls>
<source src="/getFileVideo" type='video/mp4'>
</video>
后端代碼:
/**
* description 獲取視頻文件
*
* @author yanzy
* @date 2022/12/16 17:37
*/
@GetMapping("/getFileVideo")
public void getFileVideo(HttpServletResponse response) throws IOException {
File videoFile = new File("E://test.mp4");
FileInputStream inputStream = new FileInputStream(videoFile);
ServletOutputStream out = response.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[1024];
while ((byteRead = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
out.flush();
inputStream.close();
}
PC端沒任何問題:
IOS端播放不了:
在網(wǎng)上搜索了很多辦法,有加前端參數(shù)配置的:
<video id="video" width="350" height="500" controls
muted autoplay preload loop
x5-video-player-fullscreen="true"
x5-playsinline
playsinline
webkit-playsinline
poster="test.jpg">
<source src="/getFileVideo" type='video/mp4'>
</video>
有改后端多次發(fā)送請求,分段獲取數(shù)據(jù)流的:
經(jīng)過多次測試最終找到了問題,視頻文件在ios上需要兩個參數(shù),一個是視頻文件的類型,一個是文件的大小長度:
response.setContentType("video/mp4");
response.setHeader("Content-length", String.valueOf(videoFile.length()));
修改后ios瀏覽器就可以正常播放了:文章來源:http://www.zghlxwxcb.cn/news/detail-603941.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-603941.html
到了這里,關(guān)于蘋果safari瀏覽器播放不了video標簽視頻的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!