Play YouTube videos in Android apps

Source: Internet
Author: User

1. Use 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();

You can use vedioview to play a local video file and use setvideopath.
If you play an online video, it is said that the above method can be used.
Brother doesn't believe in legends, he only believes in the effect of doing it. So there is no way to play the video in the actual test!
As a result, I learned that the normal video is played after loading, while the RTSP data stream is played while loading.
How can I get the RTSP link of a YouTube video?
Reference: http://stackoverflow.com/questions/7937903/get-rtsp-url-from-youtube
However, you only need to have a YouTube account. You can log on to the account using a single email address.
After obtaining the video RTSP, replace it with the program.
Failed !! Prompt, unable to play this video !!!
Why? Not clear. Is there a problem with Character Parsing in the string?
I have been looking for a long time and don't know why. I will give up this solution temporarily.
In addition, you can use mediaplayer to play the video file. videoview calls mediaplayer to play the video.
This solution will be available for further research in the future, because it comes to another simpler solution.
2. Use webview
In Android, webview can be used to open web pages, while YouTube provides video embedding code, allowing users to embed in their own web pages.
In this case, if you use webview to open an HTML-embedded video, can you directly play the video?
Start the experiment immediately. Generally, webview loads a URL directly. First, check whether HTML can be directly loaded using webview. The experiment results and loaddata can meet the requirements.
So with the following code

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 = "

For details about how to obtain the embedded code of a YouTube video, refer to the official website of YouTube:
Http://support.google.com/youtube/bin/answer.py? Hl = ZH-Hans & Answer = 171780
The above Code also sets the video to play automatically.
For other video attributes, refer to the official website developed by Google:
Https://developers.google.com/youtube/player_parameters#enablejsapi
In addition, the replacement of special characters can make HTML loaded correctly.
Reference: http://stackoverflow.com/questions/7125976/android-use-webview-with-loaddata
Add the following to the Code:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.