Android: playing youtube video on Webview
try this
String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.mWebView);
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
also set in manifist file android:hardwareAccelerated="true"
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
YouTube Video not playing in WebView - Android
Add these lines before loading HTML content to your WebView.
wv.setWebChromeClient(new WebChromeClient() {
});
From the documentation:
In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient. For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.
Is it possible to play a Youtube video using WebView in Android
Use the new YouTube Android Player API - https://developers.google.com/youtube/android/player
"The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience."
So for using with a Channel, you'd use the regular YoutTube API to get the metadata, and play the videos/playlists with the Player API.
How to show YouTube video in webView on Android
Try the following code: Change the URL of video in the string.
String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.mWebView);
displayYoutubeVideo . setWebViewClient ( New WebViewClient () {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo . getSettings ();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
How to play YouTube video inside Android WebView?
Probably not of any use, but it might be handy for future people
But remove this line:
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
and make sure javascript is enabled
ws.setJavaScriptEnabled(true);
These two lines caused me quite a lot of trouble when I was starting out.
play youtube video in WebView
You cannot show them embedded except perhaps on devices that have Flash.
However, if you can parse out the YouTube video details, you may be able to construct an ACTION_VIEW
Intent
that will show them on the YouTube application...for those Android devices that have the YouTube application.
You might also experiment with HTML5's <video>
tag, which AFAIK is supported in the Browser application and may therefore work in WebView
.
How to check if webview YouTube video player was started
The communication between webview and native are done through JavascriptInterface. YouTube has built it's API in a similar fashion. you can use the below code to achieve what you want.
To achieve the play/pause functionality via Youtube video you can use YouTube JavaScript Player API.
Example:
<div id="video-placeholder"></div>
<script src="https://www.youtube.com/iframe_api"></script>
When the API is fully loaded, it looks for a global function called onYouTubeIframeAPIReady()
which you should define.
Your code should look something like this
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('video-placeholder', {
width: 600,
height: 400,
videoId: 'Xa0Q0J5tOP0',
playerVars: {
color: 'white',
playlist: 'taJ60kskkns,FG0fTKAqZ5g'
},
events: {
onReady: initialize
}
});
}
Just make two buttons and call the needed method on click.
$('#play').on('click', function () {
player.playVideo();
});
$('#pause').on('click', function () {
player.pauseVideo();
});
You can use JavascriptInterface to get the callback inside the native java/kotlin code from WebView.
More details info
Youtube javascript player API with example
JavaScript Interface with example
Related Topics
Android Studio: Creating Landscape Layouts
Highlight Listview Selected Row
Kotlin Error:Could Not Find Org.Jetbrains.Kotlin:Kotlin-Stdlib-Jre7:1.0.7
Android: Notifydatasetchanged(); Not Working
Android: Gles20: Called Unimplemented Opengl Es API
How to Keep a Countdowntimer Running Even If the App Is Closed
Save Data and Change Orientation
Android Fragments: When to Use Hide/Show or Add/Remove/Replace
How to Show One Layout on Top of the Other Programmatically in My Case
Extending Relativelayout, and Overriding Dispatchdraw() to Create a Zoomable Viewgroup
Audiotrack, Soundpool or Mediaplayer Which Should I Use
Gradle Sync Failed Could Not Find Constraint-Layout:1.0.0-Alpha2
Downloading/Caching Google Maps for Offline Use
How to Change the Background of Android Alert Dialogs
Android How to Replace the Deprecated Tabhost