SoFunction
Updated on 2025-04-11

Solve the problem that video tag cannot be played automatically under Android webview

Under Android webview

html5's video sets autoplay attribute

Or use the play method in document ready

None of them can be automatically played

You can only use the onPageFinished method of webview to solve this problem, the code is as follows:
Copy the codeThe code is as follows:

(new WebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { ("javascript:(function() { var videos = ('video'); for(var i=0;i<;i++){videos[i].play();}})()"); }
});

Putting the above code in //here can solve the problem.
Copy the codeThe code is as follows:

public class PlayerActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
//here
}
}