1

我在 react-native 中使用 tus-android-client 将视频上传到 vimeo。视频正在上传,但没有在恢复时恢复,而是从 0 重新开始。

我正在关注有关可恢复上传的 Vimeo api 指南 ( https://developer.vimeo.com/api/upload/videos#resumable-tus-upload )。

这是代码。


  class TusRunnable extends TusExecutor implements Runnable {
    private TusAndroidUpload upload;
    private TusUploader uploader;
    private String uploadId;
    private String uploadEndPoint;
    private TusClient client;
    private boolean shouldFinish;
    private boolean isRunning;

    public TusRunnable(String fileUrl, String uploadId, String endpoint, Map<String, String> metadata,
        Map<String, String> headers) throws FileNotFoundException, MalformedURLException {
      this.uploadId = uploadId;
      this.uploadEndPoint = endpoint;

      client = new TusClient();
      // client.setUploadCreationURL(new URL(endpoint));

      SharedPreferences pref = reactContext.getSharedPreferences("tus", 0);

      client.enableResuming(new TusPreferencesURLStore(pref));
      client.setHeaders(headers);

      upload = new TusAndroidUpload(Uri.parse(fileUrl), reactContext);
      upload.setMetadata(metadata);
      shouldFinish = false;
      isRunning = false;
    }

    protected void makeAttempt() throws ProtocolException, IOException {
      uploader = client.beginOrResumeUploadFromURL(upload, new URL(uploadEndPoint));

      uploader.setChunkSize(1024);
      uploader.setRequestPayloadSize(10 * 1024 * 1024);

      Timer progressTicker = new Timer();

      progressTicker.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
          sendProgressEvent(upload.getSize(), uploader.getOffset());
        }
      }, 0, 500);

      do {
      } while (uploader.uploadChunk() > -1 && !shouldFinish);

      sendProgressEvent(upload.getSize(), upload.getSize());

      progressTicker.cancel();
      uploader.finish();
    }    
  }
4

0 回答 0