我正在使用我的应用程序在Vimeo上传视频,但我的部分视频状态未获得更新
我已按照此链接中提供的api文档:https://developer.vimeo.com/api/upload/videos:
我正在使用以下代码:
public boolean sendVideo(String file1, String completeURi, String endpoint, String id) throws FileNotFoundException, IOException {
File file = new File(file1);
long contentLength = file.length();
String contentLengthString = Long.toString(contentLength);
FileInputStream is = new FileInputStream(file);
int bufferSize = 20485760;
byte[] bytesPortion = new byte[bufferSize];
int byteNumber = 0;
while (is.read(bytesPortion, 0, bufferSize) != -1) {
String contentRange = Integer.toString(byteNumber);
boolean success = false;
int bytesOnServer = 0;
while (!success) {
long bytesLeft = contentLength - (bytesOnServer);
System.out.println(newline + newline + "Bytes Left: " + bytesLeft);
if (bytesLeft < bufferSize) {
//copy the bytesPortion array into a smaller array containing only the remaining bytes
bytesPortion = Arrays.copyOf(bytesPortion, (int) bytesLeft);
//This just makes it so it doesn't throw an IndexOutOfBounds exception on the next while iteration. It shouldn't get past another iteration
bufferSize = (int) bytesLeft;
}
bytesOnServer = sendVideoBytes("Vimeo Video upload", endpoint, contentLengthString, "video/mp4", contentRange, bytesPortion, first,isuploaded);
AppLog.e("bytesOnServer", "===contentLength===" + bytesOnServer +"&&=="+contentLengthString);
if (bytesOnServer >= Integer.parseInt(contentLengthString)) {
System.out.println("Success is true!");
return true;
} else {
contentRange = (bytesOnServer + 1) + "-" + (Integer.parseInt(contentLengthString)) + "/" + (Integer.parseInt(contentLengthString));
System.out.println(bytesOnServer + " != " + contentLength);
System.out.println("Success is not true!"+contentRange);
success=false;
first = true;
}
}
}
return true;
}
/**
* Sends the given bytes to the given endpoint
*
* @return the last byte on the server (from verifyUpload(endpoint))
*/
private static int sendVideoBytes(String videoTitle, String endpoint, String contentLength, String fileType, String contentRange, byte[] fileBytes, boolean addContentRange,boolean isuploaded) throws FileNotFoundException, IOException {
OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", contentLength);
request.addHeader("Content-Type", fileType);
if (addContentRange) {
request.addHeader("Content-Range", "bytes " + contentRange);
}
request.addPayload(fileBytes);
Response response = signAndSendToVimeo(request, "sendVideo with file bytes " + videoTitle, false);
if (response.getCode() != 200 && !response.isSuccessful()) {
return -1;
}
return verifyUpload(endpoint, contentLength, contentRange,isuploaded);
}
/**
* Verifies the upload and returns whether it's successful
*
* @param
* @param contentLength
* @param endpoint to verify upload to @return the last byte on the server
*/
public static int verifyUpload(String endpoint, String contentLength, String contentRange,boolean isuploaded) {
// Verify the upload
OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", "0");
request.addHeader("Content-Range", "bytes */*");
Response response = signAndSendToVimeo(request, "verifyUpload to " + endpoint, true);
AppLog.e("verifyUpload", "" + response.getCode());
if (response.getCode() != 308 || !response.isSuccessful()) {
return -1;
}
String range = response.getHeader("Range");
AppLog.e("After verify","==range header contains"+Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)));
//range = "bytes=0-10485759"
return Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)); //+1 remove
//return Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)) + 1;
//The + 1 at the end is because Vimeo gives you 0-whatever byte where 0 = the first byte
}
public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
String newline = "\n";
System.out.println(newline + newline
+ "Signing " + description + " request:"
+ ((printBody && !request.getBodyContents().isEmpty()) ? newline + "\tBody Contents:" + request.getBodyContents() : "")
+ ((!request.getHeaders().isEmpty()) ? newline + "\tHeaders: " + request.getHeaders() : ""));
service.signRequest(OAuthConstants.EMPTY_TOKEN, request);
Response response = request.send();
// AppLog.e("Uplaod Video aftre Response", "" + response.getCode());
return response;
}
任何人都可以为Android的工作代码提供帮助吗? 在此先感谢!!
答案 0 :(得分:2)
我最近和vimeo有同样的问题。 api为上传过程返回状态 VIMUploadState_Succeeded ,但是如果您尝试在 vimeo.com 上观看视频,则它会处于上传状态并显示黑屏在您尝试重现视频时在应用中。
我从他们的支持团队得到了以下答案:
看起来像这个特定的视频 没有从上传状态变为失败状态。
很抱歉,这些链接中提供的视频从未上传过 并且迷路了。他们需要重新上传。
在我们的平台上,每天都会上传几个视频,似乎没有模式可以确定何时会发生上传问题。如果您重新上传视频不是问题,那么vimeo可以是一个很好的解决方案,因为与其他视频平台相比,它的价格非常好,否则您应该寻找另一种视频存储/播放解决方案。
答案 1 :(得分:0)
如果没有适当的调试,很难检查你的代码。
以下是添加了更多功能的代码的解决方法
您可以查看代码here