【发布时间】:2017-02-22 07:53:57
【问题描述】:
我正在尝试将 mp4 文件从 sd 卡上传到远程服务器。上传成功,但是当我尝试使用 VideoView 通过 url 播放该文件时,它显示“无法播放此视频”。仅使用手机捕获的视频才会出现此问题,假设如果我从 watsapp 文件夹上传视频,一切正常,没有任何问题。在上传之前我需要做任何压缩吗? 这是我用于上传视频的代码
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(my url for upload);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
video_name = fileName;
video_name = fileName.substring(fileName.lastIndexOf("/")+1);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
Log.i("ava", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
Content = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
【问题讨论】:
-
您是否验证视频已成功上传到远程服务器?
-
@HeshanSandeepa 是的,它也可以在浏览器中播放。但感觉有些问题存在,有时会卡住。
-
那么这里真正的问题是什么? “无法播放此视频”或“卡住”??
-
@HeshanSandeepa 在网络浏览器中播放上传的视频时发生卡住。如果尝试在 Android 应用的 VideoView 中加载该 url,它将显示无法播放此视频
-
可以发一下网址吗
标签: android video upload video-streaming