10

我正在使用我的应用程序在 Vimeo 中上传视频,但我的某些视频状态没有得到更新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 的工作代码吗?提前致谢!!

4

2 回答 2

2

我最近对 ​​vimeo 有同样的问题。该 api 返回状态VIMUploadState_Succeeded用于上传过程,但如果您尝试在vimeo.com上观看视频,它会卡在上传状态,并且当您尝试重现视频时,它会在应用程序中显示黑屏。

我从他们的支持团队得到以下答案:

看起来这个特定的视频没有从上传状态变为失败状态。

抱歉,这些链接中提供的视频从未完成上传并且丢失了。他们将需要重新上传。

在我们的平台上,每天都会上传几个视频,而且似乎没有确定何时会发生此上传问题的模式。如果重新上传视频对您来说不是问题,vimeo 可能是一个很好的解决方案,因为与其他视频平台相比,它的价格确实不错,否则您应该寻找其他视频存储/播放解决方案。

于 2016-10-19T23:47:58.043 回答
0

如果没有适当的调试,很难检查你的代码。

这是您的代码的解决方法,添加了更多功能

你可以在这里查看代码

于 2016-10-18T23:41:45.450 回答