【发布时间】:2015-12-28 08:51:49
【问题描述】:
我正在尝试使用他们网站上提供的 youtubeinmp3 api 下载 youtube 视频,但问题是当我尝试转换他们库中尚不存在的歌曲时(这意味着它之前尚未转换),该网站将开始将此歌曲转换为 .mp3 格式。
现在我的 HTTPUrlConnection 发生的情况是,如果下载链接已经在网站上,因为它已被缓存,那么它可以完美运行,但如果网站必须执行转换,它会在不等待转换的情况下通过- 意思是我得到了一个错误。
这是我目前拥有的:
URL link = new URL("http://YouTubeInMP3.com/fetch/?video=http://www.youtube.com/watch?v=JbjtL7P9eI0&autostart=1");
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
HttpURLConnection httpConnection = (HttpURLConnection) (new URL("http://YouTubeInMP3.com/fetch/?video=http://www.youtube.com/watch?v=JbjtL7P9eI0&autostart=1").openConnection());
System.out.println(HttpURLConnection.HTTP_OK);
long completeFileSize = httpConnection.getContentLength();
long downloadedFileSize = 0;
//int currentProgress = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
downloadedFileSize += n;
final int currentProgress= (int) (downloadedFileSize * 100 / completeFileSize);
System.out.println("current progress: "+currentProgress);
}
System.out.println("Finished");
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream("/Users/stephen/Desktop/test.mp3");
fos.write(response);
fos.close();
我的想法是找到一种方法来停止我的程序的执行,或者让它在一个 while 循环中运行,只要网站还没有完成加载。有没有办法做到这一点?
【问题讨论】:
-
请分享错误
-
那么输出文件将是空白的,如果我检查
completeFileSize,如果必须转换文件,它会返回-1