注意,请保证你的手机已经下载 并安装adobe flash player
(http://blog.csdn.net/zircon_1973/article/details/8067041 Android WebView播放flash(判断是否安装flash插件 ))
原文:http://blog.csdn.net/lllkey/article/details/18453179
参考demo: http://download.csdn.net/download/mantianku/4831860
在播放优酷视频, 点击全屏的时候, 会黑屏, 而不显示需要全屏的view
试过各种方法, 发现有一些细节需要注意:
1. demo的Manifest Activity需要设置为: 全屏, 横屏
- <activity android:name="com.demo.flashplay.ActFlashPlay"
- android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
- android:screenOrientation="landscape" />
注意: 此处如果不设置为 横屏 点击全屏也会失败的
有的说播放flash是需要在Activity添加硬件加速的属性, 这个demo是在程序判断, 所以没有添加
2. 添加权限:
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
3. 退出视频播放会出现视频还没停止的问题:
- @Override
- public void onPause() {
- super.onPause();
- mWebView.onPause();
- }
-
- @Override
- public void onResume() {
- super.onResume();
- mWebView.onResume();
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK
- && event.getAction() == KeyEvent.ACTION_DOWN) {
- mWebView.loadData("", "text/html; charset=UTF-8", null);
- finish();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
当按下返回时, 设置Webview加载空
代码:
xml文件: act_flashplay.xml
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <FrameLayout
- android:id="@+id/fullscreen_custom_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:visibility="gone" />
-
- <FrameLayout
- android:id="@+id/main_content"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <WebView
- android:id="@+id/webview_player"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:scrollbars="none" />
- </FrameLayout >
- </FrameLayout>
java文件: ActFlashPlay.java
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebSettings.PluginState;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.FrameLayout;
-
- import com.example.myalldemo.R;
-
- public class ActFlashPlay extends Activity {
-
- private FrameLayout mFullscreenContainer;
- private FrameLayout mContentView;
- private View mCustomView = null;
- private WebView mWebView;
- private String s = "<html><head><meta charset=\"utf-8\" /><title>swf</title></head><body>"
- + "<embed src=\"http://player.youku.com/player.php/sid/XMzcyMzY2NTY4/v.swf\" bgcolor=\"#000000\""
- + " width=\"80%\" height=\"80%\" align=\"middle\" allowScriptAccess=\"always\""
- + " allowFullScreen=\"true\" wmode=\"transparent\" "
- + "type=\"application/x-shockwave-flash\"> </embed></body></html>";
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.act_flashplay);
-
- initViews();
- initWebView();
-
- if (getPhoneAndroidSDK() >= 14) {
- getWindow().setFlags(0x1000000, 0x1000000);
- }
-
- mWebView.loadData(s, "text/html; charset=UTF-8", null);
-
- }
-
- private void initViews() {
- mFullscreenContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
- mContentView = (FrameLayout) findViewById(R.id.main_content);
- mWebView = (WebView) findViewById(R.id.webview_player);
-
- }
-
- private void initWebView() {
- WebSettings settings = mWebView.getSettings();
- settings.setJavaScriptEnabled(true);
- settings.setJavaScriptCanOpenWindowsAutomatically(true);
- settings.setPluginState(PluginState.ON);
-
- settings.setAllowFileAccess(true);
- settings.setLoadWithOverviewMode(true);
-
- mWebView.setWebChromeClient(new MyWebChromeClient());
- mWebView.setWebViewClient(new MyWebViewClient());
- }
-
- class MyWebChromeClient extends WebChromeClient {
-
- private CustomViewCallback mCustomViewCallback;
- private int mOriginalOrientation = 1;
-
- @Override
- public void onShowCustomView(View view, CustomViewCallback callback) {
-
- onShowCustomView(view, mOriginalOrientation, callback);
- super.onShowCustomView(view, callback);
-
- }
-
- public void onShowCustomView(View view, int requestedOrientation,
- WebChromeClient.CustomViewCallback callback) {
- if (mCustomView != null) {
- callback.onCustomViewHidden();
- return;
- }
- if (getPhoneAndroidSDK() >= 14) {
- mFullscreenContainer.addView(view);
- mCustomView = view;
- mCustomViewCallback = callback;
- mOriginalOrientation = getRequestedOrientation();
- mContentView.setVisibility(View.INVISIBLE);
- mFullscreenContainer.setVisibility(View.VISIBLE);
- mFullscreenContainer.bringToFront();
-
- setRequestedOrientation(mOriginalOrientation);
- }
-
- }
-
- public void onHideCustomView() {
- mContentView.setVisibility(View.VISIBLE);
- if (mCustomView == null) {
- return;
- }
- mCustomView.setVisibility(View.GONE);
- mFullscreenContainer.removeView(mCustomView);
- mCustomView = null;
- mFullscreenContainer.setVisibility(View.GONE);
- try {
- mCustomViewCallback.onCustomViewHidden();
- } catch (Exception e) {
- }
-
-
- setRequestedOrientation(mOriginalOrientation);
- }
-
- }
-
- class MyWebViewClient extends WebViewClient {
-
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
-
- view.loadUrl(url);
- return super.shouldOverrideUrlLoading(view, url);
- }
-
- }
-
- public static int getPhoneAndroidSDK() {
-
- int version = 0;
- try {
- version = Integer.valueOf(android.os.Build.VERSION.SDK);
- } catch (NumberFormatException e) {
- e.printStackTrace();
- }
- return version;
-
- }
-
- @Override
- public void onPause() {
- super.onPause();
- mWebView.onPause();
- }
-
- @Override
- public void onResume() {
- super.onResume();
- mWebView.onResume();
- }
- }
注: demo中的方法是loadUrl, load的是asset文件夹下的1234.html, 可以播放视频, 同样的,上面代码中用loadData也是可以的
大家可以试一试, 有什么问题可以一起讨论
附:http://www.eoeandroid.com/thread-185654-1-1.html
本帖最后由 wangwenxin6868 于 2012-7-19 16:11 编辑 在使用webView.loadData(data, "text/html", "utf-8"),出现找不到网页的问题, 检查之后发现data数据是正确的,也有上网的权限,可是就是找不到网页, 后经过查找,找到了解决方法:把 webView.loadData 改为 webView.loadDataWithBaseURL 方法就可以了, 可以这样使用:webView.loadDataWithBaseURL(null,data, "text/html", "utf-8", null);
原因如下:
loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,出现这种字符就会出现解析错误,
显示找不到网页还有部分html代码。因为css中经常用'#', '%'等字符,所以大部分网页会出现解析错误,需要如何处理呢?我们需要用UrlEncoder编码为%23, %25, %27, %3f 。
webView.loadData(URLEncoder.encode(data, "utf-8"), "text/html", "utf-8");
这样写之后会发现错误没有了,但是显示效果不好,一些背景颜色排版效果都没有了。
public void loadDataWithBaseURL ([url=]String[/url] baseUrl, [url=]String[/url] data, [url=]String[/url] mimeType, [url=]String[/url] encoding, [url=]String[/url] historyUrl)
推荐使用loadDataWithBaseURL(),只是多了两个参数,我们可以这样使用
webView.loadDataWithBaseURL(null,data, "text/html", "utf-8", null);
这样html代码就会完美解析了。
注意,访问网络要在配置文件中加上权限
webView.getSettings().setJavaScriptEnabled(true);
让WebView 支持JavaScript。
|