【Android】How to embed Youtube videos in Webview?

窩在艾爾
·
·
IPFS
·
It is often possible to play Youtube videos directly in blog articles; but how to implement it in the app?

Want to embed Youtube videos in your App,

And it will not jump to the Youtube App to play,

How to achieve it?

Demo video


First, you need to get the embed URL of the Youtube video,

Click the share button below the Youtube video

Click share below the video

Click on the embed option

Click on the embed in the screen

At this point, you will see a series of URLs embedded in the video, click to copy and put them in the Code.

Click to copy

If you want to play automatically, you need to add " ?&autoplay=1 " after the video ID

https://www.youtube.com/embed/th9WMMJuOFU?&autoplay=1 \

This is how to prepare in advance~


Next is the part of the code

Add WebView object
Add WebView object


Load video preview image
Use WevViewAssetLoader to load movie preview images
Configure WebView
 setJavaScriptEnabled(true) // Need to set can use JavaScript
setUserAgentString(USERAGENT) // If you need to play the video automatically, UserAgent must set setWebViewClient(new WebViewClient()); // To embed the URL, you need to use WebViewClient() to Request


set url and play
 loadUrl(VideoEmbededAdress); //Set non-embedded URL to WebView, this example is embedded video, so loadUrl is not applicable loadDataWithBaseURL("", VideoEmbededAdress, mimeType, encoding, ""); //Set embedded video URL


Request playback, you will get the URL of the Request, which can be used in this Debug
Finally WebViewClient Request

After the above steps, you are done

Come and enjoy the Demo works~

Demo video

GitHub code

If you have any questions, please leave a message to discuss~

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!

窩在艾爾【問題筆記】#Linux,#Android、#Java、#Python,#LineBot 【日常隨手分享】#吃 #喝 #玩 #樂
  • Author
  • More

【CryptoTab】終止CryptoTab Pro挖礦的原因

【Ubuntu】無止盡的登入失敗

【CryptoTab】CryptoTab MAX真的有比較划算嗎?