Android-播放html5& lt; video& gt;在html代码中 [英] Android - playing html5 <video> in html code

查看:43
本文介绍了Android-播放html5& lt; video& gt;在html代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有很多旧讨论,但是我正在开发新设备.

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

我想使用WebView在视频标签中显示html5视频.我想将其用于Android设备4.0+甚至4.1+.

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+.

我不想大量更改WebView代码.是否可以在WebView中显示电影而无需在顶部打开一些新视图?因为大多数答案都打开了播放视频的新视图?我需要尽可能少的代码更正的简单解决方案.

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.

已更新

清单:

<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>

主文件:

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;
    }
}

推荐答案

以下是我针对Android API15及更高版本的应用程序的片段,该片段播放受支持的视频类型(.mp4是最可靠的).它可以提供来自URL的内容,也可以内嵌创建(注释掉).

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.

清单中需要以下内容

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

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

然后是代码:

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");
}
}

这篇关于Android-播放html5&amp; lt; video&amp; gt;在html代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆