ButterBeast
ButterBeast

Reputation: 551

Android - playing html5 <video> in html code

I know there are a lot of old discussion about this, but I am developing for new devices.

I want to use WebView to show html5 video in video tag. I want to use this for android devices 4.0+ or even 4.1+.

I do not want to change the WebView code a lot. Is it possible to show movie in WebView without opening some new views on top? Because most of the answers opened new view to play video? I need easy solution with as less as possible code corrections.

Any code or tutorial will be welcome.

UPDATED

manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videoexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:hardwareAccelerated="true"
    >


    <activity
        android:name="com.example.videoexample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Main-file:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebSettings;

public class MainActivity extends Activity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webView = (WebView) findViewById(R.id.webView1);

        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
        webView.getSettings().setJavaScriptEnabled(true);

        // load the customURL with the URL of the page you want to display
        String pageURL = "http://www.vongola-restavracija-izola.si/powzyLibrary1/video.html";
        webView.loadUrl(pageURL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Upvotes: 1

Views: 7528

Answers (3)

Offbeatmammal
Offbeatmammal

Reputation: 8238

the following is a snippet from an application I have for Android API15 and above that plays supported video types (.mp4 is the most reliable). It can serve content from a URL or created in-line (commented out).

If, with this, the video still won't play, can you post a link to a sample here so we can check if it's compatible.

You need the following in the manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and

<application
android:hardwareAccelerated="true" 
.... />

and then the code:

package com.offbeatmammal.android.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.RelativeLayout;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);

    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
    webView.getSettings().setJavaScriptEnabled(true);

    // load the customURL with the URL of the page you want to display
    // String pageURL = "http://url/page.html";
    // webView.loadUrl(pageURL);

    String customHtml = "<html><head><title>Sample</title></head><body><video controls><source src='http://www.broken-links.com/tests/media/BigBuck.m4v'></video></body></html>";
    webView.loadData(customHtml, "text/html", "UTF-8");
}
}

Upvotes: 1

user3274355
user3274355

Reputation: 36

I started working off https://code.google.com/p/html5webview/

Video works there. Make sure your video is compatible with android

Upvotes: 1

Sanket Kachhela
Sanket Kachhela

Reputation: 10876

set this in meanifeast.xml

<application android:hardwareAccelerated="true">

hardwareAccelerated works in above 3.0. so you can play video in WebView.

refer this android docs

Upvotes: 0

Related Questions