我正在开发一款允许本地离线观看视频内容的android应用程序。
我正在尝试使用他们的API从Vimeo下载视频,我遇到了一个问题,我认为这个问题与url本身和它的身份验证有关。
我被告知URL是HTTP 302重定向。
我已经用来自的popeye视频测试了我的程序https://archive.org/download/Popeye_forPresident/Popeye_forPresident_512kb.mp4并且工作正常。但当我放入Vimeo链接时,它甚至无法连接。
这是运行实际连接的方法。
public String DownloadFile(String fileURL, String fileName) {
InputStream in = null;
BufferedInputStream inStream = null;
FileOutputStream out = null;
HttpURLConnection connection = null;
try {
File FileDir = mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES);
URL url = new URL(fileURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
//Code stops here with vimeo video but not with popeye video.
...
out = new FileOutputStream(new File(FileDir, fileName + ".mp4"));
in = connection.getInputStream();
inStream = new BufferedInputStream(in, 1024 * 5);
byte[] buffer = new byte[1024 * 5];
int len;
while ((len = inStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
... //catch and finally { flush, close and disconnection}
return null;
}
这是设置url并执行它的异步任务
class DownloadFileFromURL extends AsyncTask<String, String, String> {
...
@Override
protected String doInBackground(final String... args) {
//If it is a normal direct link such as site.com/video.mp4 (the popeye video)
//Set link to site.com/video.mp4 and save as "args[1]" on disk.
if(FilenameUtils.isExtension(args[0], "mp4")){
DownloadFile(args[0], args[1]);
}
//Else if a vimeo link (which it is)
else if (args[0].contains("vimeo")){
final int pos = Integer.parseInt(args[2]);
//Use Vimeo API to get the videos from Vimeo.
VimeoClient.getInstance().fetchNetworkContent(CHANNELS_VIDEO_URI, new ModelCallback<VideoList>(VideoList.class) {
@Override
public void success(VideoList videoList) {
if (videoList != null && videoList.data != null && !videoList.data.isEmpty()) {
Video video = videoList.data.get(pos);
ArrayList<VideoFile> videoFiles = video.files;
if(videoFiles != null && !videoFiles.isEmpty()) {
VideoFile videoFile = videoFiles.get(0);
//Set link to the link that you retrieve.
//Example: http://player.vimeo.com/external/175231540.hd.mp4?s=78206f99af10e32354851f6ca62252782b1393fb&profile_id=174&oauth2_token_id=903072961
String link = videoFile.link;
DownloadFile(link, args[1]);
}
}
}
@Override
public void failure(VimeoError error) {
Toast.makeText(mContext, "Failure in VideoList Reading", Toast.LENGTH_LONG).show();
}
});
}
return null;
}
...
}
但联系http://player.vimeo.com/external/175231540.hd.mp4?s=78206f99af10e32354851f6ca62252782b1393fb&profile_id=174&oauth2_token_id=903072961未连接到。我尝试删除hd.mp4之后的所有内容,但我认为这不起作用,因为在浏览器中转到该链接会导致授权拒绝错误。
我如何连接到此链接并使用令牌从HTTP 302重定向中获取文件?
一些事情。。。
我认为你不应该假设传递给doInBackground的位置在网络呼叫获取视频列表后会是相同的位置。如果一个新的视频被添加到该列表中,该怎么办?
什么是CHANNELS_VIDEO_URI
?只有Vimeo PRO成员可以访问文件阵列,并且只能访问他们自己的视频。您正在使用的Vimeo网络库的ReadMe中提到了这一点。这是你自己的视频频道吗?
你评论中的URL看起来不正确,它代表的是播放器,而不是视频文件。我有一部分不相信URL来自VideoFile.link
files数组还可能包含mp4文件以外的链接,因此您也应该对此保持警惕。
每个视频对象上都有一个download
阵列。它类似于files
,只是这些应该是到实际视频资源的链接(而且你不会得到HLS!)。只有当视频的设置允许下载视频时,这些选项才可用。