gpt4 book ai didi

android - 在 Android 中使用 YouTube Data API v3 可恢复 YouTube 上传

转载 作者:行者123 更新时间:2023-11-29 00:24:15 25 4
gpt4 key购买 nike

我正在使用 YouTube Data API v3 从我的 Android 应用程序将视频上​​传到我的 YouTube 帐户.上传过程完美无缺。我需要的是,如果我的上传被中断(例如,由于互联网连接),我需要在下次上传时从它离开的地方开始上传。

private void uploadVideo(UploadInfo videoInfo) {

Insert videoInsert = prepareUpload( new File(videoInfo.getFilePath()) );

new VideoUploadAsyncTask(videoInfo, getActivity(), videoUploadAsyncTaskInterface).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, videoInsert);
}

public Insert prepareUpload( File videoFile ) {

try {
Video videoObjectDefiningMetadata = new Video();

VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);

VideoSnippet snippet = new VideoSnippet();

snippet.setTitle(videoFile.getName());
snippet.setDescription("Uploaded via AnujAroshA Android app");

List<String> tags = new ArrayList<String>();
tags.add("AnujAroshA");
snippet.setTags(tags);

videoObjectDefiningMetadata.setSnippet(snippet);

InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, new BufferedInputStream(new FileInputStream(videoFile)));
mediaContent.setLength(videoFile.length());

YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,status", videoObjectDefiningMetadata, mediaContent);

MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

uploader.setDirectUploadEnabled(false);

uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);

return videoInsert;
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
}
}

我该怎么做?


使用 VideoUploadAsyncTask 类的 doInBackground 方法更新

@Override
protected String doInBackground( Insert... inserts ) {

try{

Insert videoInsert = inserts[0];

MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {

@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {

switch (uploader.getUploadState()) {
case INITIATION_STARTED:
Log.d(TAG, "# INITIATION_STARTED ");
break;

case INITIATION_COMPLETE:
Log.d(TAG, "# INITIATION_COMPLETE ");
break;

case MEDIA_IN_PROGRESS:
int progress = (int) Math.round(uploader.getProgress() * 100);
Log.d(TAG, "# MEDIA_IN_PROGRESS : progress = " + progress + "%");

publishProgress(progress);
break;

case MEDIA_COMPLETE:
Log.d(TAG, "# MEDIA_COMPLETE ");
publishProgress(100);
break;

case NOT_STARTED:
Log.d(TAG, "# NOT_STARTED ");
break;

default:
break;
}
}
};

uploader.setProgressListener(progressListener);

Video returnedVideo = videoInsert.execute();

return returnedVideo.getId();

}catch(GooglePlayServicesAvailabilityIOException gpsaioe){

}catch(UserRecoverableAuthIOException uraioe){

}catch(GoogleAuthIOException gaioe){

}catch(IOException ioe){

}
return null;
}

最佳答案

你见过this question吗? ?

答案建议你应该使用;

youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {          
@Override
public void initialize(HttpRequest request) throws IOException {
credential.initialize(request);
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
}
});

当您初始化 YouTube 对象时。

关于android - 在 Android 中使用 YouTube Data API v3 可恢复 YouTube 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com