问题描述
我知道有很多关于此的旧讨论,但我正在为新设备开发。
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<视频>在HTML代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!