how to play video in android webview? - android

I want to put a web into webview.These is a video in the web.The video's format is wmv.I put the web in the android project "assets" file.Now I can scan the web,but the video can't play.This is the code on below.Who can help me?Thanks.
public class WebViewActivity extends Activity {
private WebView webview;
private WebSettings webSettings;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webView);
webSettings = webview.getSettings();
webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setBuiltInZoomControls(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("file:///android_asset/demo/html/demo.html");
webview.setWebViewClient(new MyWebClient());
}
private class MyWebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

WMV is a proprietary Microsoft video format. Chances are Android doesn't have a codec for it. You should convert it to something standard e.g H264.
Unfortunately video encoding is a minefield of proprietary tech. There are a few open source converters available. FFMPEG is probably the most extensive. There's a handy GUI for this called WinFF (if you're using windows).
You can test your HTML file by putting it and your video onto the SDcard and see if it works in it's simplest form. Try with different video formats as a sanity check.

WMV isn't widely supported on Android devices, but there are various tools to convert format.
https://gist.github.com/3718414 is a sample showing an Android webview and HTML5 player that works with the video codecs that Android supports (.mp4 is best for progressive video as it has by far the widest support)

Related

Google/Android TV WebView vs FireTV WebView output

I am trying to play a video on WebView. The URL for the video is https://presenter.displaynow.io/video?url=https://dnmediaservice-usea.streaming.media.azure.net/eba08e4d-3f77-4343-8206-360541683fc5/21bf46b6-c6ec-925c-d5d2-7b6b61b2.ism/manifest
On Google and Android TV it is working as expected but on Amazon FireTV, the video is hiding behind some black container. On the Silk browser which was preinstalled on FireTV, the video is working as expected.
I tried to find the issue but I am not able to find any issue with my code. The HTML generated from the silk browser and my application WebView is also similar except for some minor changes.
Below is my code.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://presenter.displaynow.io/video?url=https://dnmediaservice-usea.streaming.media.azure.net/eba08e4d-3f77-4343-8206-360541683fc5/21bf46b6-c6ec-925c-d5d2-7b6b61b2.ism/manifest");
this.setContentView(webView);
}
}
Google/Android/Silk WebView output
FireTV WebView output

How to play youtube video inside a floating popup window in android

I can create a floating popup window using standout library https://github.com/pingpongboss/StandOut . As I understand this library creates a service which creates a popup window.
Youtube API for Android provides YouTubePlayerView, which can be only used with YoutubeBaseActivity, and which I cannot use because the floating popup window belongs to a service.
Youtube API also provides YoutubePlayerFragment, which also cannot be used because services cannot work with fragments.
There is also WebView, which can be used here, which is not as nice as Youtube API for Android, but better than nothing. It almost works for me. The WebView is displayed inside a floating popup window, and the video is being played, but the video is just black.
Here is WebView code
private static final String FRAME_VIDEO = "<html><body>Video From YouTube<br><iframe width=\"320\" height=\"280\" src=\"https://www.youtube.com/embed/_oEA18Y8gM0\" frameborder=\"0\"></iframe></body></html>";
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webView.setWebChromeClient(new WebChromeClient());
webView.loadData(FRAME_VIDEO, "text/html", "utf-8");
Maybe one of these options is possible:
Somehow use YoutubePlayerView without YouTubeBaseActivity
Use fragments without activity
Fix the WebView code
Maybe there is some other way to play youtube videos inside a floating popup window

Trying to autoplay YouTube video in a PhoneGap/Cordova app

I'm developing an app using PhoneGap. So, it's a web app. In this app, I have a requirement to embed a YouTube video and autoplay it whenever the user navigates to that particular page.
I've read that HTML5 video autoplay doesn't work on mobile devices because of bandwidth concers. My question is that is there any way at all to bypass this restriction? I don't mind complex workarounds or hacks that could allow me to do this. Anything at all.
Thanks.
As you pointed out yourself, all autoplay instructions in your code itself will be ignored on load. So we'll implement a function that gets the video in the body and starts playing it.
The following javascript code could do this:
(function() {
document.getElementsByTagName('video')[0].play();
})()
To execute this code after the page has loaded, we need to set a WebViewClient and implement onPageFinished()
webview.setWebViewClient(new CordovaWebViewClient(this, webview) {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
A full example:
webview = new CordovaWebView(this);
setContentView(webview);
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new CordovaWebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
webview.loadUrl("http://html5demos.com/video");
There is a cordova/phonegap plugin that promises to do this. It rely on the Webview method setMediaPlaybackRequiresUserGesture, added in API level 17 (Android 4.2).
It simply call this method on the webview:
WebView view = getWebViewFromPlugin();
view.getSettings().setMediaPlaybackRequiresUserGesture(false);
It's also available in phongap build.

How to run the HTML5 video in webview in android

From last one week i try to find how to play a html5 video in android webview i read so many blog and questions in the stack overflow but my problem is solve.i am using .mp4 , .wevm ,ogv and also .3gp format but noon of format is running.
i run these videos in the two different device one is micro-max funbook with android 4.0 and the another device is a Samsung device with 2.3.3 android os.
this is my code.
url="file://"+Environment.getExternalStorageDirectory()+"/unzipped/HTML5/res/test1.html";
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(url);
For API level 11+ it should suffice that you add the attribute android:hardwareAccelerated="true" to the <application> tag in your Android manifest file.
For older APIs you need to implement the methods onShowCustomView and onHideCustomView in your your own WebChromeClient.
Or you can check out the html5webview

ExternalInterface.addCallback not working on Android for local files

I have the following situation:
AS3 Code:
...
Security.allowDomain("*");
ExternalInterface.addCallback("onZoom", onZoom);
...
public function onZoom(...
JavaScript code:
...
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
...
And in OBJECT and EMBED tags I have allowScriptAccess="always"
If I test the html page from my computer ("file:///path-to-file.html") both
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
give me proper result.
If I put my html as well as my swf in my android phone sdcard and I do load it into a WebView like this...
webView = (WebView) findViewById(R.id.webViewMain);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///sdcard/test/test.html");
... then alert(getFlashMovie("test").onZoom); gives me "undefined".
If I move my html and swf to a web server, and load with webView.loadUrl("http:/mydomain.com/test/test.html"); it works.
I did another try with this example:
http://0me.me/demo/adobeflash/ExternalInterface.call/demo.html
It works if I load it from that url, it doesn't if I copy that html file (and the relevant swf) on my sdcard.
If I copy the same file (and swf) on my computer and test it locally, there it works.
So it seems that flash player for computer allows ExternalInterface.addCallback even for local files (file:///...) whereas flash player for android 10.2 doesn't.
I didn't try installing a local web server on my android phone and loading from there so far, but I would avoid such a solution if possible.
Can someone help me please?
Thanks.

Categories

Resources