【Android】How to embed Youtube videos in Webview?
IPFS
Want to embed Youtube videos in your App,
And it will not jump to the Youtube App to play,
How to achieve it?
First, you need to get the embed URL of the Youtube video,
Click the share button below the Youtube video
Click on the embed option
At this point, you will see a series of URLs embedded in the video, click to copy and put them in the Code.
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
Load video preview image
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
After the above steps, you are done
Come and enjoy the Demo works~
If you have any questions, please leave a message to discuss~
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!
- Author
- More