flash数据交互
Posted Faxcom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flash数据交互相关的知识,希望对你有一定的参考价值。
安卓中webview读取html,同时嵌入Flex的SWF,交互
安卓activity与html交互很简单,用javascript接口即可,网上一堆的例子,基本上没多大问题。
在html里面嵌入swf并与之交互就有点麻烦,我用了ExternalInterface没有成功,那位兄台成功了可以交流交流。我用的是FlashVars,
不用改什么配置,html就可以向swf传递数据。
- <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="940" height="600">
- <param name="movie" value="client.swf?v=3">
- <param name="wmode" value="opaque"/>
- <param name="quality" value="high" />
- <param name="FlashVars" value="p1=222&p2=333" />
- <param name="menu" value="false"/>
- <embed src="client.swf" quality="high" wmode="opaque" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" FlashVars="p1=222&p2=333" width="940" height="600">
- </embed>
- </object>
其中可以在FlashVars中指定,也可以通过movie或src中的swf url指定参数,例如上面的v。
Flex代码
- // 需要引入
- import mx.core.Application;
- // 获取 FlashVars 的 Object
- var params:*= Application.application.parameters;
- // 也可以指定某一个参数
- var p1:String = Application.application.parameters.p1;
Okay!
android使用Webview播放Swf文件,实现与Flash数据交互
HDSwfPlayer
谷歌中国API链接:https://developer.android.google.cn
支持swf播放以及html带swf的播放。
支持swf与js的交互。
自动写入flash信任路径。
提供播放回调。
Android版本不要超过4.3。
目录
如何导入到项目
支持jcenter方式导入。
支持本地Module方式导入。
jcenter方式导入
- 在需要用到这个库的module中的build.gradle中的dependencies中加入
- 1
- 2
- 3
Module方式导入
- 下载整个工程,将hdmediaplayer拷贝到工程根目录,settings.gradle中加入
- 1
- 在需要用到这个库的module中的build.gradle中的dependencies中加入
- 1
- 2
- 3
如何使用
本类支持播放.swf文件、.html文件(.html可以包裹.swf文件并实现与android的交互)。
在demo中提供.html文件模板实例,如果需要js与android数据交互,请移步demo参考。
HDSwfPlayerHelper
- 初始化
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 为了让退出播放或者在播放时用户转到其它页面后flash不再播放,应该重写用于播放的Activity的onPause和onResume方法,并分别调用webview的隐藏方法”onPause”和”onResume
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 更多的操作
- 1
- 2
- 3
关于我
欢迎 Star Fork交流地址:尹海德(123302687@qq.com)- 1
- 2
- 3
License
Copyright 2017 yinhaideLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Github传送门https://github.com/yinhaide/HDSwfPlayer
- 本文已收录于以下专栏:
- Android开源之路
Android通过javascript与flash动画交互
问题描述:当我们在Android应用上加载了flash,然后希望点击flash的相关控件,Android应用能够做出相应。
这就涉及到Android通过javascript与flash动画交互技术。我们实现方案是:新建一个html文件显示flash动画,html文件嵌入javascript函数与flash交互,然后我们我们Android应用通过webview加载html文件,再与该html文件嵌入的javascript函数交互,最后可以实现Android的数据与flash的数据进行通信。有点绕口,不知读者是否看明白了。总之就一句话:html中的javascript函数是中介,Android与flash分别与它进行数据传输即可。
贴代码:
flash.html
[html] view plain copy- <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd ;">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script language="JavaScript" type="text/JavaScript">
- var str1 = "hello";
- var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
- function myFlash_DoFSCommand(command, args) {
- var myFlashObj = InternetExplorer ? myFlash : document.myFlash;
- //flash传来的参数,如果有数据传来则调用Android代码的函数,将flash数据传给Android
- if(command =="file1_open"||command=="file2_open"||command=="file3_open"||command=="file4_open"||command=="file5_open"||command=="file6_open")
- {
- //alert(command);
- doFromCommand(command);
- }else{
- //alert(command);
- }
- }
- //下面的代码是网页加载flash的钩子
- if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
- navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
- document.write(\'<SCRIPT LANGUAGE=VBScript\\> \\n\');
- document.write(\'on error resume next \\n\');
- document.write(\'Sub myFlash_FSCommand(ByVal command, ByVal args)\\n\');
- document.write(\' call myFlash_DoFSCommand(command, args)\\n\');
- document.write(\'end sub\\n\');
- document.write(\'</SCRIPT\\> \\n\');
- }
- //调用android的函数,runJs2Activity是Android代码定义的函数,<span style="font-family: Arial, Helvetica, sans-serif;">playerJs是Android中定义该类的别名,</span><span style="font-family: Arial, Helvetica, sans-serif;">目的是获得flash传来的数据</span>
- function doFromCommand(command){
- window.playerJs.runJs2Activity(command);
- }
- </script>
- </head>
- <body>
- <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
- codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=11,0,0,0"
- WIDTH="100%" HEIGHT="100%" id="myFlash">
- <PARAM NAME=movie VALUE="flash.swf"> <!--这里一堆代码是加载flash动画的-->
- <PARAM NAME=quality VALUE=high>
- <PARAM NAME=bgcolor VALUE=#CCCCCC>
- <param name="allowScriptAccess" value="always" />
- <param name="allowNetworking" value="all">
- <param name="allowFullScreen" value="true">
- <EMBED src="flash.swf" quality=high bgcolor=#CCCCCC WIDTH="100%" HEIGHT="100%" NAME="myFlash" swLiveConnect="true" allowScriptAccess="always" allownetworking="all" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
- </OBJECT>
- <div id="show" style="color:#000000;font-size:30px;margin-left:50px" ></div>
- </body>
- </html>
[java] view plain copy- //类中类:定义js中调用的Android类
- lass RunJavaScript {
- public void runJs2Activity(String str) {
- strFromJs = str;
- mScriptHandler.removeCallbacks(mPlayerRunnable);
- mScriptHandler.postDelayed(mPlayerRunnable, 300);
- }
[java] view plain copy- //给网页文件添加Android与JS交互函数的定义
- mWebView.addJavascriptInterface(new RunJavaScript(), "playerJs");
下面是Android完整代码[java] view plain copy- package com.ideal.swfplayer;
- /************************
- *用来加载flash.html文件*
- ************************/
- import java.io.File;
- import java.util.Timer;
- import java.util.TimerTask;
- import sunvision.database.DBOperation;
- import sunvision.dialog.MatchDialog;
- import sunvision.dialog.TipDialog;
- import sunvision.dialog.VersionDialog;
- import sunvision.file.FileOperation;
- import sunvision.tools.FlashPath;
- import sunvision.tools.IdealSystemProperties;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.os.SystemClock;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- 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 android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainFlashActivity extends Activity {
- private WebView mWebView = null;
- private FrameLayout mFrameLayout = null;
- private ImageView mImageView = null;
- private Timer mTimer = null;
- private Timer mTimer2 = null;
- private WebSettings settings;
- private String strFromJs = "";
- private String TAG = "SwfPlayer";
- private int mLoadingIndex = 0;
- private int mWebViewLoadTimes = 0;
- private int versionloadtime=0;
- private MatchDialog mDialogManager;
- private Handler mHandler = null;
- private FileOperation mFileUnit;
- private boolean isversionup=true;
- Handler mScriptHandler = new Handler() {};
- //加载驱动
- public native void native_initPlayer( ) throws Exception;
- public native void native_prepare() throws Exception;
- public native void native_start();
- public native void native_finish();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_flash);
- //初始化UI界面
- InitUI();
- //加载主界面
- loadSwf(FlashPath.MainPath);
- //等待消息传过来更新界面
- //mHandler.post(task); //启动定时器1秒刷新一次
- mHandler = new Handler() {
- @SuppressLint("Recycle")
- public void handleMessage(Message msg) {
- Log.i(TAG, "PlayerActivity-mLoadingIndex==" + mLoadingIndex);
- switch (msg.arg1) {
- case 0:
- mImageView.setImageResource(R.drawable.flashloading);
- break;
- case 1:
- mTimer.cancel();
- mImageView.setVisibility(View.GONE);
- mImageView.destroyDrawingCache();
- mLoadingIndex = 0;
- break;
- case 6:
- mWebView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, mWebView.getLeft() + 5,mWebView.getTop() + 5, 0));
- mWebView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,mWebView.getLeft() + 5, mWebView.getTop() + 5, 0));
- break;
- default:
- break;
- }
- super.handleMessage(msg);
- }
- };
- }
- //UI界面初始化函数
- private void InitUI() {
- mFileUnit = new FileOperation(getApplicationContext());
- mFileUnit.createFlashPlayerTrust();
- mFrameLayout = (FrameLayout) findViewById(R.id.player_frameLayout);
- mImageView = (ImageView) findViewById(R.id.swf_loading_img);
- mWebView = (WebView) findViewById(R.id.webView_show);
- }
- //加载Flash动画
- @SuppressLint({ "SetJavaScriptEnabled", "Recycle" })
- public void loadSwf(String swfPath) {
- //获得配置函数
- settings = mWebView.getSettings();
- //设置允许与js交互
- settings.setJavaScriptEnabled(true);
- //设置允许文件操作
- settings.setAllowFileAccess(true);
- //设置允许使用Adobe Flash播放视频
- settings.setPluginState(PluginState.ON);
- //设置加载方式是替换加载,而不是新页面加载
- settings.setLoadWithOverviewMode(true);
- //设置编码方式
- settings.setDefaultTextEncodingName("GBK");
- //设置透明背景
- mWebView.setBackgroundColor(0);
- //重写Flash加载辅助函数
- mWebView.setWebChromeClient(new WebChromeClient() {
- public void onShowCustomView(View view, int requestedOrientation,
- WebChromeClient.CustomViewCallback callback) {
- super.onShowCustomView(view, callback);
- //Android SDK版本
- if (Build.VERSION.SDK_INT >= 14) {
- if (view instanceof FrameLayout) {
- mFrameLayout.addView(view,new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,Gravity.CENTER));
- mFrameLayout.setVisibility(View.VISIBLE);
- }
- }
- }
- @Override
- public void onHideCustomView() {
- super.onHideCustomView();
- }
- });
- //重写Flash加载主要函数
- mWebView.setWebViewClient(new WebViewClient() {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
- //Flash启东时调用
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- super.onPageStarted(view, url, favicon);
- mWebViewLoadTimes++;
- }
- //Flash加载完成之后调用
- @Override
- public void onPageFinished(WebView view, String url) {
- super.onPageFinished(view, url);
- //Flash加载之前会有一段时间是白屏,为了掩盖,让五张图片隔时刷新
- if (mWebViewLoadTimes == 1) {
- mTimer = new Timer();
- mTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- Message msg = new Message();
- msg.what = 0;
- msg.arg1 = mLoadingIndex;
- mHandler.sendMessage(msg);
- mLoadingIndex++;
- }
- }, 0, 2000);
- //flash偶尔会失去焦点,让flash加载完成之后每隔2秒点击一次界面获得焦点
- mTimer2 = new Timer();
- mTimer2.schedule(new TimerTask() {
- @Override
- public void run() {
- Message msg = new Message();
- msg.what = 0;
- msg.arg1 = 6;
- mHandler.sendMessage(msg);
- }
- }, 0, 2000);
- } else if (mWebViewLoadTimes >= 2) {
- mWebViewLoadTimes = 0;
- String path = FlashPath.TIP_NO_FILE;
- Intent intent = new Intent(MainFlashActivity.this,NoFileFlashActivity.class);
- intent.putExtra("swfPath", path);
- startActivity(intent);
- MainFlashActivity.this.finish();
- }
- }
- });
- //给网页文件添加Android与JS交互函数的定义
- mWebView.addJavascriptInterface(new RunJavaScript(), "playerJs");
- //设置网页能够获得焦点
- mWebView.requestFocusFromTouch();
- mWebView.requestFocus();
- mWebView.setFocusable(true);
- //设置完毕之后加载Flash
- mWebView.loadUrl(swfPath);
- }
- //类中类:定义js中调用的Android类
- class RunJavaScript {
- public void runJs2Activity(String str) {
- strFromJs = str;
- mScriptHandler.removeCallbacks(mPlayerRunnable);
- mScriptHandler.postDelayed(mPlayerRunnable, 300);
- }
- }
- //类中接口:当js调用RunJavaScript时,新开辟线程处理消息,这里是接口
- Runnable mPlayerRunnable = new Runnable() {
- @Override
- public void run() {
- //文件操作行为
- if (strFromJs.equals("file1_open")||strFromJs.equals("file2_open")||strFromJs.equals("file3_open")||strFromJs.equals("file4_open")||strFromJs.equals("file5_open")||strFromJs.equals("file6_open") ) {
- File folder = new File("/mnt/external_sd/");
- //TF卡存在
- if(folder.length()>0){
- Intent intent = new Intent(MainFlashActivity.this,FileListActivity.class);
- intent.putExtra("filename", strFromJs);
- startActivity(intent);
- //TF卡不在
- }else{
- TipDialog mTipDialog = new TipDialog(MainFlashActivity.this, R.style.IdealDialog,R.drawable.no_tf_card,3);
- //设置背景透明度
- WindowManager.LayoutParams lp=mTipDialog.getWindow().getAttributes();
- lp.dimAmount=0.7f;
- mTipDialog.getWindow().setAttributes(lp);
- mTipDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- mTipDialog.show();
- }
- }
- }
- };
- //加载Antivity是调用
- @SuppressLint("Recycle") @Override
- protected void onResume() {
- super.onResume();
- //flash在回到该Activity是能够继续播放
- try {
- mWebView.getClass().getMethod("onResume").invoke(mWebView, (Object[]) null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //Activity停止时调用
- @Override
- protected void onPause() {
- super.onPause();
- //允许flash暂停播放
- try {
- mWebView.getClass().getMethod("onPause").invoke(mWebView, (Object[]) null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //该Activity销毁时调用
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mWebView.destroyDrawingCache();
- mWebView.destroy();
- try {
- mHandler.removeMessages(0);
- mTimer.cancel();
- } catch (Exception e) {
- }
- }
- }
Android加载flash时有一些要注意的问题:Webview加载flash一开始会出现白屏,可以通过刷图的形式掩盖;刚加载完flash,它还无法获得Android用户的焦点事件,必须在代码里面模拟点击屏幕,才可以获得焦点;在运行flash的过程中可能会出现焦点丢失的情况,我的方案是每隔几秒点击屏幕一次;Android加载flash需要flash插件,必须确保插件装好了,但是。即使插件装好了也不能通信,必须要把要加载的flash的路径写到flash插件安装目录下的安全路径文件中,这点非常重要,如读者遇到该问题,需要帮助的话可在线留言~
以上是关于flash数据交互的主要内容,如果未能解决你的问题,请参考以下文章