通过chooseVideo接口,用手机录制视频(超过3s),然后获取返回的临时视频路径(tempFile.tempFilePath),将临时路径赋给video组建的src属性,在开发模拟器中可以通过该路径播放视频,可以正常播放,但是手机端不能正常播放。
复现方法:
1、使用小程序搜索:“企业文件宝”
2、点击云文档,上传视频,然后录制视频(超过3s),再点击播放,可以复现改问题
小程序代码
视频上传代码
wx.chooseVideo({
sourceType: [‘album’, ‘camera’],
compressed: true,
maxDuration: 60,
camera: ‘back’,
success: function (res) {
var tempFile = res;
var vedioName = (new Date()).getTime() + “.mp4”;
wx.uploadFile({
url: “/api/v2/uploadVideo”, //仅为示例,非真实的接口地址
filePath: tempFile.tempFilePath,
name: vedioName,
success: function (res) {
consol.log(“上传成功”);
});
});
});
视频播放代码
<view class=“section”>
<video src="/api/v2/downloadFile?fileName=123.mp4" style=‘width:100%;’ autoplay=‘true’ custom-cache="{{isCache}}" controls></video>
</view>
服务器代码
上传代码
@RequestMapping(value = “/uploadVideo”, method = RequestMethod.POST)
public void uploadVideo(HttpServletRequest request) throws Exception {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);
fileUpload.setHeaderEncoding(“UTF-8”);
List<FileItem> list = fileUpload.parseRequest(request);
Map<String,String> map = new HashMap<String,String>();
String fileName = null;
for (FileItem fileItem : list) {
if(fileItem.isFormField()){
//如果是表单项
String name = fileItem.getFieldName();
String string = fileItem.getString(“utf-8”);
//表单项的集合
map.put(name, string);
}else{
//上传项
fileName = fileItem.getName();
InputStream is = fileItem.getInputStream();
// 获得文件要上传的路径(后面的路径可以自定义):
String pathPre = “/opt/tomcat_web/”;
String filePath = pathPre + fileName;
OutputStream os = new FileOutputStream(filePath);
byte[] byts = new byte[1024];
int len = 0;
while ( (len = is.read(byts)) != -1 ) {
os.write(byts, 0, len);
os.flush();
}
is.close();
os.close();
}
}
}
下载代码
@RequestMapping(value = “/downloadFile”, method = RequestMethod.GET)
public void downloadFile(@RequestParam String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception{
// 下载文件,从服务器端读取文件,将文件内容写回客户端
String pathPre = “/opt/tomcat_web/”;
String filePath = pathPre + fileName;
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
String header = “attachment; filename=\”" + fileName + “\”";
response.setHeader(“Content-Disposition”, header);
response.setContentType(“application/octet-stream; charset=utf-8”);
OutputStream out = response.getOutputStream();
int temp;
while ((temp = in.read()) != -1) {
out.write(temp);
}
out.close();
in.close();
}