android - 在Android上下载youtube

标签 android youtube

阅读以下内容后:Code for download video from Youtube on Java, Android

我找到了一种获取youtube下载链接的方法,该方法与此类似:
http://o-o---preferred---lax02s10---v9---lscache4.c.youtube.com/videoplayback?upn=7XKwMrZklvQ&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=903903%

我尝试使用下载视频

    public void DownloadFromUrl(String DownloadUrl, String fileName) {
    Log.i("DownloadFromUrl","DownloadFromUrl"+DownloadUrl);

       try {
               File root = android.os.Environment.getExternalStorageDirectory();               

               File dir = new File (root.getAbsolutePath() + "/youlike");
               if(dir.exists()==false) {
                    dir.mkdirs();
               }

               URL url = new URL(DownloadUrl); //you can write here any link
               File file = new File(dir, fileName);

               long startTime = System.currentTimeMillis();
               Log.d("DownloadManager", "download begining");
               Log.d("DownloadManager", "download url:" + url);
               Log.d("DownloadManager", "downloaded file name:" + fileName);


               /* Open a connection to that URL. */
               URLConnection ucon = url.openConnection();
               Log.i("URLConnection","URLConnection Succeed");

               /*
                * Define InputStreams to read from the URLConnection.
                */ 
               InputStream is = ucon.getInputStream();
               BufferedInputStream bis = new BufferedInputStream(is);
               Log.i("BufferedInputStream","BufferedInputStream Succeed");

               /*
                * Read bytes to the Buffer until there is nothing more to read(-1).
                */
               ByteArrayBuffer baf = new ByteArrayBuffer(5000);
               int current = 0;
               while ((current = bis.read()) != -1) {
                  baf.append((byte) current);
                  Log.i("ByteArrayBuffer","ByteArrayBuffer Succeed");

               }


               /* Convert the Bytes read to a String. */
               FileOutputStream fos = new FileOutputStream(file);
               Log.i("FileOutputStream","FileOutputStream initialized");
               fos.write(baf.toByteArray());
               Log.i("fos.write(baf.toByteArray());","fos.write(baf.toByteArray()); initialized");
               fos.flush();
               fos.close();
               Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

       } catch (IOException e) {
           Log.d("DownloadManager", "Error: " + e);
       }

    }

但是它向我返回了一条错误消息:java.io.FileNotFoundException
我测试了该网址,并确定它可以正常工作。谁能告诉我问题是什么?

最佳答案

打开http连接之前,您应该对youtube下载链接进行编码吗?

关于android - 在Android上下载youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12339356/

相关文章:

android - Android Auto 中未列出的应用程序

android - 当我单击主页按钮时,android应用程序未关闭

youtube - 如何使用YouTube数据API在特定位置获取自特定日期以来上传的YouTube视频以及观看次数?

java - 如何制作一个网格,其中每个属性都是另一个 TextView 下方的 TextView

android - 用户多个设备上的 Google Cloud Messaging 通知

java相当于python正则表达式

c# - 需要一些建议通过 asp.net 网站控制 C# winforms 应用程序

javascript - 如何在该网页标题的帖子中执行给定数据

javascript - 类型错误 : Cannot read property 'snippet' of undefined

android - 为什么 Firebase JobDispatcher 运行 JobService 2 次?