How to Stop Youtube Video Playing in Android Webview

How to stop youtube video playing in Android webview?

See the following post about WebView threads never stopping

Essentially you'll need to call the WebView's onPause method from your own Activity's onPause method.

The only trick with this is that you cannot call the WebView's onPause method directly because it is hidden. Therefore you will need to call it indirectly via reflection. The following code should get you started on setting up your own Activity's onPause method:

@Override
public void onPause() {
super.onPause();

try {
Class.forName("android.webkit.WebView")
.getMethod("onPause", (Class[]) null)
.invoke(webview, (Object[]) null);

} catch(ClassNotFoundException cnfe) {
...
} catch(NoSuchMethodException nsme) {
...
} catch(InvocationTargetException ite) {
...
} catch (IllegalAccessException iae) {
...
}
}

Note that the variable 'webview' in the try block above is a private instance variable for the class and is assigned to in the Activity's onCreate method.

How to play and pause video playing on Android WebView iframe?

for pause video in android studio web view you can use webview.onPause first set onscroll change listener for recycler view and pause web view when recycler view scrolled. But their will be a problem if you place image view over webview for thumbnail then you won't get play event.
So it will be good and very useful if you will customize the webview events. that will be very helpful.
Thank You fr the help. :-)

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>

WebView - Youtube videos playing in background on rotation and minimise


Pausing/Resuming the Video

The simple way to do this is to pause the WebView when you wish the video to stop playing (i.e. when the app goes into the background). This is simply accomplished by the following code:

WebView view = (WebView) findViewById(R.id.webView);
view.onPause(); // This will pause videos and needs to be called for EVERY WebView you create
view.pauseTimers(); // This will pause JavaScript and layout for ALL WebViews and only needs to be called once to affect all WebViews

The best place to handle this logic is to put your pausing code in the onPause() method of your Activity, and to resume the WebView when the activity comes back in view of the user in onResume:

WebView mWebView; // Initialize this somewhere

@Override
protected void onPause(){
super.onPause();
if(mWebView != null){
mWebView.onPause();
mWebView.pauseTimers();
}
}

@Override
protected void onResume(){
super.onResume();
if(mWebView != null){
mWebView.onResume();
mWebView.resumeTimers();
}
}

You may be interested in the onPause and onResume as well as the pauseTimers/resumeTimers methods in the WebView.

Handling Orientation Changes

If you don't want your Activity to restart when you rotate the screen (which will stop the WebView from being recreated) then just put

android:configChanges="orientation|screenSize|keyboardHidden|keyboard"

in the <activity> tag of your Activity in your AndroidManifest file. This is not a good practice to follow normally, but when you are using a complex native view like the WebView, it is okay to do (Android web browsers use this to avoid being recreated on rotate). What will happen when using this tag is that the layout will just be resized to fit the landscape orientation and you will have to manually handle any layout specific changes that were being handled using -port or -land folders in your resources. If you are only using one layout though then it is not a problem.

Please see this question/answer for more info on this, and note that it is not encouraged but it will work.

Android - webview, disable or replace youtube videos


$(document).ready(function() {
$('iframe').each(function() {
var src = $(this).attr('src');
// Replace youtube vids
var ytprefix = "http://www.youtube.com/embed/";

if(src.indexOf(ytprefix) != -1) {
replaceYT(this, src.substring(ytprefix.length));
}
});

$('object').each(function() {
var srcel = $('param[name="src"]', this);
var src = $(srcel).attr('value');
// Replace youtube vids
var ytprefix = "http://www.youtube.com/v/";
if(src.indexOf(ytprefix) != -1) {
replaceYT(this, src.substring(ytprefix.length));
}
});
});

function replaceYT(el, code) {
if(code.indexOf("/") != -1) {
code = code.substring(0, code.indexOf("/"));
}
if(code.indexOf("?") != -1) {
code = code.substring(0, code.indexOf("?"));
}
var atag = $("<a href='vnd.youtube:" + code +"'><img class='youtubeimg' src='file:///android_asset/youtube-play-button.png' style='background:url(http://img.youtube.com/vi/" + code + "/0.jpg)'/></a>");
$(el).replaceWith(atag);
}

Just load in jquery and this script. This will replace youtube iframes and object tags with a screenshot from the video and a link to youtube. Tested on a SE Xperia X10 & Nexus 7.

Set up your webview to handle youtube like this:
WebViewClient mWebClient = new WebViewClient() {

    @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http://www.youtube.com") || url.startsWith("vnd.youtube")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}

};


Related Topics



Leave a reply



Submit