在ANDROID程式中播放youtube視頻

來源:互聯網
上載者:User

1.使用VedioView

  VideoView videoView = (VideoView)findViewById(R.id.video);  String src = "rtsp://v6.cache7.c.youtube.com/CjYLENy73wIaLQlSNwWysTZuKRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYP6mvvezrYeZTgw=/0/0/0/video.3gp";  videoView.setVideoURI(Uri.parse(src));  videoView.setMediaController(new MediaController(this));  videoView.requestFocus();  videoView.start();

   使用VedioView可以播放本地視頻檔案,使用setVideoPath
   如果播放網路視頻的話,據傳說上面的方法可以。
   哥不相信傳說,哥只相信幹出來的效果。於是實際測試,沒有辦法播放!
   於是到網上爬文,得知一般的視頻播放是先載入完再播放,而RTSP格式的資料流,是邊載入邊播放。
   如何得到youtube視頻的rtsp連結呢?
   參考網址:http://stackoverflow.com/questions/7937903/get-rtsp-url-from-youtube
   不過先要有youtube的帳號才可以,可以隨便用一個郵箱登入。
   這樣得到視頻rtsp後,替換到程式中。
   失敗!!提示,無法播放此視頻!!!
   是什麼原因?不清楚。是不是字串中的字元解析出了問題呢?
   找了很久不知道原因,暫時放棄這個解決方案。
   另外,使用MediaPlayer也可以播放視頻檔案,videoview是調用MediaPlayer實現視頻播放的。
   這個方案以後有時間再研究,因為想到另外一個更加簡便的解決方案。
2. 使用WebView
   android中可以利用WebView來開啟網頁,而youtube提供視頻內嵌程式碼,允許使用者在自己的網頁嵌入。
    這樣的話,如果用WebView來開啟html格式嵌入的視頻,是不是可以直接播放視頻呢?
    馬上開始實驗,一般WebView都是直接load一個url,那先調查下使用WebView可否直接載入html,實驗結果,loadData可以滿足要求。
    於是有了下面的代碼

browser = (WebView) findViewById(R.id.webview);   browser.requestFocus();   browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);   WebSettings setting = browser.getSettings();   setting.setJavaScriptEnabled(true);   setting.setSupportZoom(true);   setting.setBuiltInZoomControls(true);   setting.setAllowFileAccess(true);   setting.setJavaScriptCanOpenWindowsAutomatically(true);   setting.setAppCachePath("/data/data/" + getPackageName() + "/app_path/");   setting.setAppCacheEnabled(true);   setting.setUserAgent(0);   setting.setLoadsImagesAutomatically(true);   setting.setSavePassword(true);   setting.setLightTouchEnabled(true);   setting.setPluginsEnabled(true);   setting.setPluginState(PluginState.ON);   setting.setUserAgentString("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; zh-tw) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16");       browser.setWebViewClient(new WebViewClient());   html = "<html><body>hello,hello!" +       "<iframe width=\"1120\" height=\"630\" src=\"http://www.youtube.com/embed/XseHQvZw1vA?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>" +     "</body></html>";   html = html.replace("?", "%3f");   browser.loadData(html, "text/html", "utf-8");

    如何取得youtube視頻的內嵌程式碼,可以參考youtube的官方網站介紹:
   http://support.google.com/youtube/bin/answer.py?hl=zh-Hans&answer=171780
    上面的代碼中還設定了視頻可以自動播放。
    視頻的其他屬性的設定可以參考google開發的官方網頁:
   https://developers.google.com/youtube/player_parameters#enablejsapi
    另外,關於特殊字元的替代,可以使html正確載入。
    參考:http://stackoverflow.com/questions/7125976/android-use-webview-with-loaddata
    在代碼中加入:

html = html.replace("%", "%25");html = html.replace("#", "%23");html = html.replace("\\", "%27");html = html.replace("?", "%3f");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.