How can we play YouTube embeded code in an Android application using webview?
Check out the following SO post:
How to embed a YouTube clip in a WebView on Android
Try the following code which may help you. I just framed it for your reference
myWebView = (WebView) findViewById( R.id.webview_compontent );
String playVideo= "<html><body>Youtube video .. <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"
myWebView.loadData(playVideo, "text/html", "utf-8");
Refer the following link for how to use Webview: http://developer.android.com/reference/android/webkit/WebView.html
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 embed a YouTube clip in a WebView on Android
I am not sure if this is what you need. Anyway I hope the following be useful.
You can use the iframe method that youtube provides to play its videos. If the browser supports html5 will show the video with it, otherwise with flash.
You can use the following code as an example <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/bIPcobKMB94" frameborder="0">
in the above example the video id is bIPcobKMB94. You can change this id and show your video.
You can access a live example of it here
More infromation for youtube iframe
YouTube HTML5 Video Player
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>
Android WebView - Youtube videos not playing within the application
Go to Google Developer Console and select or create a new project.
On the left sidebar, select APIs under APIs & auth and turn the status ON for YouTube Data API v3.
On the left sidebar, select Credentials and Create new key under Public API access.
When popup comes asking you to choose platform, select Android Key.
Paste the SHA-1 key and your project’s package name separated by semicolon(;).
Click on create. Now you should see the API KEY on the dashboard.
download YouTubeAPI jar from here
https://developers.google.com/youtube/android/player/downloads/
Create a layout like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
Your activity should look something like this
public class CustomYouTubePlayer extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private String API_KEY="your key";
private String VIDEO_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** attaching layout xml **/
setContentView(R.layout.youtube_player);
/** Initializing YouTube player view **/
VIDEO_ID = getIntent().getExtras().getString(FinalVariables.YOUTUBE_ID);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setShowFullscreenButton(false);
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.loadVideo(VIDEO_ID);
}
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
finish();
}
@Override
public void onVideoStarted() {
}
};
}
Just pass the video id and that's all simple as that..
Edit
if this is your YouTube link https://www.youtube.com/watch?v=rql_F8H3h9E
then your video_id=rql_F8H3h9E
Extract your video id from YouTube link and sent to this activity as extra variable.
Control a youtube video in WebView Programmatically
You can look at how I'm doing it in this library: android-youtube-player.
You can start by looking at the WebView class.
The idea of calling webview.loadUrl("javascript:playVideo()");
is correct. But you need to wait for the IFrame library to be loaded and ready.
Embed YouTube video within Android webview with autoPlay
I develop an part of an application using Youtube. Unfortunately, I didn't manage to open Youtube Video with autoplay by loading them in a WebView.
Just open it in the Youtube application and when the user will press the back key, it will return to yours.
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.
Related Topics
What Is Metadata ? and What Is The Use of It in Android
Android: Listview.Getscrolly() - Does It Work
How to Handle App Lifecycle with Flutter (On Android and iOS)
Placeholder Text for an Input Type="Number" Does Not Show in Webkit Ics
Automatic Versioning of Android Build Using Git Describe with Gradle
Using Katzer Local Notification in Ibm Worklight
Android Approach for "Rate My Application"
Tracking Progress of Multipart File Upload Using Okhttp
Integrating Video File in Android App as App Background
How to Add Firebase-Admin to My Android Project
How to Get Rid of Incremental Annotation Processing Requested Warning
Firebase Changing Layout of Child Data Information in Android
How to Programmatically Include Layout in Android
Getting the Pixel Color Value of a Point on an Android View That Includes a Bitmap-Backed Canvas