2013-07-03 37 views
3

有很多這樣的問題,我已經查看了所有這些問題,但沒有人有正確的答案,或正確的答案是過時的,由於YouTube的API更改。 在走出文檔領域找出最佳答案之前,我想問問你的流式傳輸YouTube視頻的方式是什麼?我認爲使用YouTube播放器API和限制用戶安裝的YouTube應用程式是非常可怕的做法流Youtube視頻沒有Youtube應用程序

使用方法建議下面我得到和「抱歉,該視頻無法播放」,甚至通過我可以使用YouTube播放器API

播放
07-03 16:58:47.139: D/MediaPlayer(20518): getMetadata 
07-03 16:58:47.139: E/MediaPlayerService(82): getMetadata failed -38 
07-03 16:58:47.680: E/ARTSPConnection(82): err = 111 (Connection refused) 
07-03 16:58:47.680: I/MyHandler(82): connection request completed with result -111 (Connection refused) 
07-03 16:58:47.740: E/MediaPlayer(20518): error (1, -2147483648) 
07-03 16:58:47.740: E/MediaPlayer(20518): Error (1,-2147483648) 
07-03 16:58:47.740: D/VideoView(20518): Error: 1,-2147483648 

回答

3

new VideoAsyncTask()。execute(); //打電話在視頻播放視頻

private class VideoAsyncTask extends AsyncTask<Void, Void, Void> { 
    ProgressDialog progressDialog; 
    String videoUrl; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = ProgressDialog.show(GallaryActivity.this, "", 
       "Loading Video wait...", true); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      // 

      String id = videosBean.getV_youtubelink(); //id of video 

      String url = "http://www.youtube.com/watch?v=" + id; 
      videoUrl = getUrlVideoRTSP(url); 
      Log.e("Video url for playing=========>>>>>", videoUrl); 
     } catch (Exception e) { 
      Log.e("Login Soap Calling in Exception", e.toString()); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     progressDialog.dismiss(); 
     /* 
     * videoView.setVideoURI(Uri.parse(
     * "rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQk4RDShYkdS1BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" 
     *)); videoView.setMediaController(new 
     * MediaController(AlertDetail.this)); videoView.requestFocus(); 
     * videoView.start(); 
     */ 

     if (myvideoview.isPlaying()) 
      myvideoview.stopPlayback(); 
     myvideoview.setVideoURI(Uri.parse(videoUrl)); 
     MediaController mc = new MediaController(GallaryActivity.this); 
     myvideoview.setMediaController(mc); 
     myvideoview.requestFocus(); 

     myvideoview.start(); 
     mc.show(); 
    } 

} 

public static String getUrlVideoRTSP(String urlYoutube) { 
    try { 
     String gdy = "http://gdata.youtube.com/feeds/api/videos/"; 
     DocumentBuilder documentBuilder = DocumentBuilderFactory 
       .newInstance().newDocumentBuilder(); 
     String id = extractYoutubeId(urlYoutube); 
     URL url = new URL(gdy + id); 
     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     Document doc = documentBuilder.parse(connection.getInputStream()); 
     Element el = doc.getDocumentElement(); 
     NodeList list = el.getElementsByTagName("media:content");// /media:content 
     String cursor = urlYoutube; 
     for (int i = 0; i < list.getLength(); i++) { 
      Node node = list.item(i); 
      if (node != null) { 
       NamedNodeMap nodeMap = node.getAttributes(); 
       HashMap<String, String> maps = new HashMap<String, String>(); 
       for (int j = 0; j < nodeMap.getLength(); j++) { 
        Attr att = (Attr) nodeMap.item(j); 
        maps.put(att.getName(), att.getValue()); 
       } 
       if (maps.containsKey("yt:format")) { 
        String f = maps.get("yt:format"); 
        if (maps.containsKey("url")) { 
         cursor = maps.get("url"); 
        } 
        if (f.equals("1")) 
         return cursor; 
       } 
      } 
     } 
     return cursor; 
    } catch (Exception ex) { 
     Log.e("Get Url Video RTSP Exception======>>", ex.toString()); 
    } 
    return urlYoutube; 

} 

protected static String extractYoutubeId(String url) 
     throws MalformedURLException { 
    String id = null; 
    try { 
     String query = new URL(url).getQuery(); 
     if (query != null) { 
      String[] param = query.split("&"); 
      for (String row : param) { 
       String[] param1 = row.split("="); 
       if (param1[0].equals("v")) { 
        id = param1[1]; 
       } 
      } 
     } else { 
      if (url.contains("embed")) { 
       id = url.substring(url.lastIndexOf("/") + 1); 
      } 
     } 
    } catch (Exception ex) { 
     Log.e("Exception", ex.toString()); 
    } 
    return id; 
} 
+0

剛剛嘗試過,請參閱更新後的問題 – Datenshi

+0

感謝您的支持!我正在爲OUYA開發一款應用程序,該應用程序沒有Youtube應用程序(因此無法使用普通的Youtube API)。您的解決方案直接從盒子中運行! –

+0

我認爲這不再有效。 'https://gdata.youtube.com/feeds/api/videos /'返回字符串'不再可用' –

-2

@Datenshi你有沒有想過使用Android的YouTube API。他們有一個偉大的playerview,它給你很多控制和事件聆聽能力的視頻。我最近發佈了一個使用API​​的教程,如果它可能適合您的需求,請查看here