2015-01-21 7 views
30

के साथ YouTube वीडियो प्लेबैक मुझे YouTube वीडियो प्लेबैक के लिए ExoPlayer का उपयोग करने में रूचि है। मैं ExoPlayer samples से देखता हूं कि वे DASH URL के माध्यम से YouTube वीडियो चलाते हैं।एक्सपोप्लेयर

मैं वीडियो खोजने के लिए एंड्रॉइड यूट्यूब एपीआई का उपयोग कर रहा हूं, और किसी भी खोज परिणामों के लिए एक DASH यूआरएल प्राप्त करने के साधन नहीं देख रहा हूं। क्या किसी को एक्सपीप्लेयर (हार्डकोडेड वीडियो यूआरएल का उपयोग किए बिना) के साथ यूट्यूब एपीआई (v3) को एकीकृत करने के लिए किसी भी नमूने से अवगत है, या वीडियो एपीप्लेयर में वीडियो लोड करने के लिए यूट्यूब एपीआई से मुझे जो जानकारी चाहिए, उसे प्राप्त करने के तरीके के बारे में पता है? पहले ", पार्स फ़ाइल मिलता है। http://www.youtube.com/get_video_info?&video_id=" + VIDEOID (नमूना "BU2zodCxwwo)" dashmpd "और इसका इस्तेमाल

प्रारंभ यूआरएल dashmpd, अंत यूआरएल:

+2

यह जांचें आपके लिए उपयोगी हो सकता है https://github.com/florent37/TutosAndroidFrance/tree/master/MyYoutube –

उत्तर

5

डैश URL प्राप्त करने के लिए, आप फ़ाइल को डाउनलोड करने की जरूरत है। &

उदाहरण (dashmpd = http://manifest.googlevideo.com/api/manifest/dash/fexp/3300133%2C3300164%2C3312381%2C9416126%2C9418044%2C9418777%2C9419452%2C9420096%2C9420452%2C9422596%2C9423291%2C9423455%2C9423661%2C9423662%2C9426963%2C9427247%2C9427888%2C9428559%2C9428564%2C9429237%2C9429515/mm/31/source/youtube/expire/1456756908/itag/0/upn/5xR9ZCMatkY/mn/sn-ov8vuxaxjvh-v8ce/key/yt6/ipbits/0/hfr/1/id/o-AIiY1RGtClDFVTCNTuhp8pRSGDPgiBHby0Il52tFnHix/sparams/as%2Chfr%2Cid%2Cip%2Cipbits%2Citag%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cplayback_host%2Csource%2Cexpire/sver/3/pl/16/s/3640F01A917CDAD260DD0BB27CE627BB9A113ED1.3CEF418955ADD2F9C2048C289AD8D0E1FB6D5F034034/ms/au/mv/m/playback_host/r1---sn-ov8vuxaxjvh-v8ce.googlevideo.com/mt/1456735204/as/fmp4_audio_clear%2Cwebm_audio_clear%2Cfmp4_sd_hd_clear%2Cwebm_sd_hd_clear%2Cwebm2_sd_hd_clear/ip/37.193.113.79)

यह URL च या एक्सएमएल फ़ाइल, जहां वीडियो के बारे में जानकारी। यह यूआरएल थोड़ा सा जीवन है और सभी वीडियो में प्रारूप fmp4 नहीं है। यदि आप पुराने यूआरएल या वीडियो का उपयोग करते हैं तो एफएम 4 प्रारूप नहीं है, तो आप 403 त्रुटि लेते हैं (आपके क्लाइंट को यूआरएल प्राप्त करने की अनुमति नहीं है)। इस समस्या का समाधान, मुझे नहीं मिला है।

public static Observable<String> downloadSound(String youtubeUrl, final String baseDir) { 

     return DownloadManager.downloadFile("http://www.youtube.com/get_video_info?&video_id=" + youtubeUrl, baseDir + File.separator + FILEYOUTUBE, new DownloadManager.IDownloadProgress() { 
      @Override 
      public void onProgress(long current, long max) { 

      } 
     }) 
       .subscribeOn(Schedulers.newThread()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .map(new Func1<File, String>() { 
        @Override 
        public String call(File youtubeFile) { 
         String dashmpd = null; 
         BufferedReader br = null; 
         try { 
          br = new BufferedReader(new FileReader(youtubeFile.toString())); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
         try { 
          StringBuilder sb = new StringBuilder(); 
          String line = br.readLine(); 

          while (line != null) { 
           sb.append(line); 
           line = br.readLine(); 
          } 

          String everything = sb.toString(); 

          Log.d("TAG", everything); 
          dashmpd = getQueryParams(everything); 

         } catch (IOException e) { 
          e.printStackTrace(); 
         } finally { 
          try { 
           br.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
         return dashmpd; 
        } 
       }); 
    } 

    public static String getQueryParams(String url) { 
     String dashUrl1 = url.substring(url.lastIndexOf("dashmpd")); 
     String dashUrl2 = dashUrl1.substring(dashUrl1.lastIndexOf("dashmpd"), dashUrl1.indexOf("&")); 
     String dashUrl = null; 
     try { 
      dashUrl = URLDecoder.decode(dashUrl2.substring(dashUrl2.indexOf("http")), "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 

     return dashUrl; 
    }